1

There is a question like mine in the link below:

creating python package with multiple level of folders

However, after following the answers to the question and trying some other things that I thought might work I did not succeed.

I have created a working package with a number of functions here:

https://github.com/aaronengland/prestige

In the prestige directory is an init.py file containing some classes and functions. I have a class named preprocessing and I can call any of the functions from that class using:

from prestige import preprocessing as pre

And then (for example):

pre.Binaritizer()

However, I want to be able to import those functions using:

import prestige.preprocessing as pre

Using the first link (above) I was unsuccessful in doing this. I feel like it should be a simple solution, but for some reason I have not been able to get it to work. Can someone please show me how to make this possible? Thank you in advance!

Aaron England
  • 1,223
  • 1
  • 14
  • 26
  • Everything defined inside the `preprocessing` class should be moved to either a separate file called `preprocessing.py` or into a `__init__.py` file inside the subdirectory `preprocessing` which would be inside the main `prestige` source directory. I would strongly recommend taking time to review the file organizational structure many open source Python projects out there such as [`django`](https://github.com/django/django/tree/master/django) or heck, [`scikit-learn`](https://github.com/scikit-learn/scikit-learn) and take some hints from them. – metatoaster Feb 12 '20 at 02:27
  • @metatoaster I have been fiddling with that file structure in a sister package at *https://github.com/aaronengland/prestige_v2* but it has not worked for me yet. Is there anything in the ```setup.py``` file that needs to change when I have multiple directories? – Aaron England Feb 12 '20 at 02:38
  • You might be looking for instructions on building [namespace packages in Python3](https://stackoverflow.com/questions/41621131/python-namespace-packages-in-python3). – metatoaster Feb 12 '20 at 02:41

1 Answers1

0

I was able to solve the problem by organizing the file structure as follows:

  • prestige
  • setup.py
  •     init.py
  •     general.py
  •     preprocessing.py

setup.py was set up as I normally do, general.py contains functions/classes, and preprocessing.py contains functions/classes. The init.py file contains 2 lines of code:

from .preprocessing import * and from .general import *

So, I did not create new directories, I just divided my functions into separate .py files and imported them into my init.py file.

Now, I am able to import functions using, for example:

from prestige.preprocessing import Binaritizer

Hopefully this helps someone in the future with a similar question.

The package can be accessed here.

Aaron England
  • 1,223
  • 1
  • 14
  • 26