Home > Net >  Ignoring the base folder name when importing a class
Ignoring the base folder name when importing a class

Time:10-05

I have a python library with the structure:

my_project <-- Base folder
 |
  -- my_project <-- Source folder
 |    
  ----- my_project.py <-- contains class MyProject
 |   
  ----- __init__.py
 |
  -- tests

Currently I have to reference the project as:

from my_project.my_project import MyProject

My setup.cfg:

[options]
packages = find:

[options.packages.find]
include = my_project

What I can't figure out is how to change the imports so that it only requires a single reference to my_project. Looked everywhere and tried multiple combinations including changing my_project to src.

from my_project import MyProject

CodePudding user response:

I think you're confusing your base folder with the source folder. Currently, you're using two references to my_project but neither reference points to the base folder.

from my_project.my_project import MyProject
        ^           ^
the source folder   |
                    |
               the my_project.py file

You're not referencing the base folder anywhere. This is how you should reference modules and classes in your code in the first place, there's nothing wrong with this.

  • Related