1

I have following directory structure of a python package i want to build (python3.4)

```
/project/src/mypackage/__init__.py
/project/src/mypackage/module.py
/project/src/mypackage/setup_utils.py
/project/setup.py
```

I have some useful code within setup_utils.py that i want to import at the top of setup.py. If I don't add sys.path.append('src'), I get ImportError

```

$ cat setup.py

from setuptools import setup
from mypackage import setup_utils
cmdclass = setup_utils.cmdcass
...

$ python setup.py install
...
ImportError: No module named 'mypackage'
...
```

Now, If I do add sys.path.append('src'), I don't get coverage Coverage.py warning: Module mypackage was previously imported, but not measured.

```
$ cat setup.py

import sys
sys.path.append('src')
from setuptools import setup
from mypackage import setup_utils
cmdclass = setup_utils.cmdcass
...

$ python setup.py install
$ coverage ...
Coverage.py warning: Module mypackage was previously imported, but not measured.

```

So, what is the right approach to fix this?

amulllb
  • 3,036
  • 7
  • 50
  • 87
  • 2
    You don't want to import a ton of code into setup.py. Setup.py has to run to install your program. You won't have any of your prerequisites installed when that happens. This can turn into a mess. – Ned Batchelder Jan 13 '18 at 04:05
  • One example of such package is `pyautogui` that can't be installed before all the dependencies are installed (in a separate pass), and can't be installed in a headless mode at all. All of that because its `setup.py` imports the `pyautogui` module to read the version, while `pyautogui/__init__.py` imports a ton of code, ranging from dependencies that may not yet be installed, to stuff that checks if there's an X display available, raising an exception if not. – hoefling Jan 15 '18 at 22:00

1 Answers1

-3

I believe your problem is identified here: Importing files from different folder

Basically, python only looks for modules within your current working directory. Since /src is not a module, it doesn't continue looking further.

Once possible solution is to turn the /src directory into a module, and then setup_utils can be a submodule within it. In order to turn src into a module, simply add an empty __init__.py file.

Then you can import it in your script as from src.mypackage import setup_utils

Kaiser Dandangi
  • 230
  • 2
  • 8