0

Problem

I have read this post, which provides a way to permanently avoid the sys.path hack when importing names between sibling directories. However, I followed the procedures listed in that post but found that I could not import installed package (i.e. test).

The following are things I have already done

  • Step1: create a project that looks like following. Both __init__.py are empty.
test
├── __init__.py
├── setup.py
├── subfolder1
│   ├── __init__.py
│   ├── program1.py
├── subfolder2
│   ├── __init__.py
│   └── program2.py
# setup.py
from setuptools import setup, find_packages

setup(name="test", version="0.1", packages=find_packages())
# program1
def func1():
    print("I am from func1 in subfolder1/func1")
# program2
from test.subfolder1 import program1

program1.func1()
  • Step2. create virtual environment in project root directory (i.e. test directory)
    • conda create -n test --clone base
    • launch a new terminal and conda activate test
    • pip install -e .
    • conda list and I see the following, which means my test project is indeed installed in the virtual environment
...
test                      0.1                       dev_0    <develop>
...
  • Step3: go to the subfolder2 and python program2.py, but unexpectedly it returned
ModuleNotFoundError: No module named 'test.subfolder1'

The issue is I think test should be available as long as I am in virtual environment. However, it does not seem to be the case here.

Could some one help me? Thank you in advance!

Mr.Robot
  • 349
  • 1
  • 16

1 Answers1

1

You need to create an empty __init__.py file in subfolder1 to make it a package.

Edit:

You should change the import in program2.py:

from subfolder1 import program1

Or you can move setup.py a level up.

Stanislas Morbieu
  • 1,721
  • 7
  • 11
  • Thank you for your help! But I tried but the problem persists. – Mr.Robot Nov 17 '19 at 00:51
  • Thank you so much! This works. But I am so confused with those import syntax (for example, why I need to move `setup.py` a level up). Could you provide a pointer to relevant docs/tutorials so that I could avoid similar mistakes. – Mr.Robot Nov 17 '19 at 01:21
  • Also, when I launch ipython interactive environment and `import test` in `test` folder. I could not call `test.program1.subfolder1.func1()` and the error `AttributeError: module 'test' has no attribute 'subfolder1'` will be returned. – Mr.Robot Nov 17 '19 at 01:25
  • 1
    The use of ``test`` perturbs things since it is a Python module in standard library. If you change the folder name to ``mypkg`` for instance, you will be able to import ``mypkg.subfolder1``. Note that you need to write ``import mypkg.subfolder1`` and not ``import mypkg`` to use ``mypkg.subfolder1``. – Stanislas Morbieu Nov 17 '19 at 01:48
  • 1
    You need to move ``setup.py`` a level up since ``find_packages()`` finds packages in subfolders. I recommend the following documentation: https://packaging.python.org/ – Stanislas Morbieu Nov 17 '19 at 01:50