0

I am playing around with the ability to create custom packages in Python. To do this I have created an extremely simple package. The directory structure is:

__init__.py
myfol1/
  __init__.py
  myfol2/
    __init__.py
    myfol3/
      __init__.py
      myfuncs.py
.gitignore
setup.py

Contents of setup.py:

import setuptools

setuptools.setup(
    name='myfol1',
    version='0.0.3',
    packages=['myfol1']
)

Contents of myfuncs.py:

def calc_addition(a,b):
    return a+b

All of the __init__.py files are empty.

Now I am trying to include the above in a different repo. To do this I have told this other repo to install the package, by adding the following line to my requirements.txt. The below line just tells it to install from the Git repo at https://github.com/eliot1785/import_example. (The reason I'm doing that is that in actuality I am testing the process for installing from a private GitHub repo, so putting it in PyPI isn't an option here.):

git+https://github.com/eliot1785/import_example.git@0.0.3

After running pip install -r requirements.txt I can see the package being installed.

Now here is the problem. import myfol1 works, or at least it doesn't raise an error in VSCode. But the moment I try to do what I actually want to do, which is add the subdirectories like from myfol1.myfol2.myfol3.myfuncs import calc_addition, it won't work because the import cannot be resolved. Similarly as soon as I type import myfol1.myfol2 I start seeing an error in VSCode.

This seems so simple but I'm obviously missing something. I can't figure out where I have gone wrong. Every answer I've seen so far suggests that adding __init__.py would be sufficient, but it just isn't working for me. Any help you can provide would be greatly appreciated.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Stephen
  • 8,508
  • 12
  • 56
  • 96
  • 1
    Having `setup.py` and `__init__.py` in the same directory is a red flag that there might be something wrong with your project directory structure. `packages` in `setup.py` should list all packages and sub-packages, not just the top-level one. – sinoroc Apr 05 '23 at 22:38
  • THANK YOU!!!!! I am new to packaging and the tutorial didn't mention that we needed to list all sub-packages in packages, because its example didn't have any sub-packages. Guess there is no substitute for the manual. I will add this as an answer in case anybody else comes across this. – Stephen Apr 05 '23 at 22:46
  • I recommend you delete the `__init__.py` at the project's root, it seems like you want `myfol1` to be the project's one and only top-level importable package (and module), so no need for `__init__.py` at the project's root. -- And I recommend that you set in the `setup.py` script `packages=setuptools.find_packages()`. -- But really you should rather write a `pyproject.toml` file instead of a `setup.py` script: https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html – sinoroc Apr 05 '23 at 22:46
  • Does this answer your question? [Why do I need to include sub-packages in setup.py](https://stackoverflow.com/questions/50083553/why-do-i-need-to-include-sub-packages-in-setup-py) – phd Apr 06 '23 at 07:23

1 Answers1

1

In packages in my setup.py, I needed to list the sub-packages and not just the top-level package. Like so:

packages=['myfol1','myfol1.myfol2','myfol1.myfol2.myfol3']

Also I have removed the __init__.py at the top level.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Stephen
  • 8,508
  • 12
  • 56
  • 96