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!

3
  • 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 or heck, scikit-learn and take some hints from them. Commented Feb 12, 2020 at 2:27
  • @metatoaster I have been fiddling with that file structure in a sister package at 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? Commented Feb 12, 2020 at 2:38
  • You might be looking for instructions on building namespace packages in Python3. Commented Feb 12, 2020 at 2:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.