-1

I guess my question is a duplicate, but, unfortunately, I haven't found a solution that corresponds to my problem.

I have following project structure:


↓ project_root
  ↓ source_root
      __init__.py
    ↓ inner_package
        some_executable_file.py
        some_library_file.py

So I would like to import a name from 'some_library_file' in the following way:

from source_root.inner_package.some_library_file import X

But when I do something like this, I see the following error:

ModuleNotFoundError: No module named 'source_root'

1 Answers1

0

You need to install your own project to your (ideally virtual) environment. In order to do this, you need a setup.py file, which will be used by pip to find all your packages (i.e., folders containing __init__.py) during installation.

Minimal example:

project_root/setup.py

from setuptools import setup, find_packages

setup(
    name='MyProjectName',
    version='0.0.0',
    packages=find_packages(),
)

Then from the command line, cd into your project_root ant type:

python -m pip install -e .

You now installed your own project into your Python environment. Files in the project are able to import their own packages with the from source_root.[...] import [...] syntax.

This process mimics a user pip installing your project from PyPI / github, so you can be sure that your imports are going to work on any other environment/machines, provided pip is used for installation.

The -e flag is used to install the project in editable mode, so you won't need to reinstall the package locally after changing one of the source files (which you'll be doing a lot during development).

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • 1
    Are you saying you always need to install your project to be able to import your own modules? – Yevhen Kuzmovych Jun 09 '22 at 13:09
  • @YevhenKuzmovych that's certainly not the only way, but it's the best way I've found in order to keep things consistent across multiple machines and environments. Other solutions tend to include `sys.path / PYTHONPATH` hacking, and may or may not work depending on the environment specifics (i.e. what's the current working directory, whether you're running the script from a terminal vs VS Code vs PyCharm, ...). See e.g. https://stackoverflow.com/a/1893622/11161432 – jfaccioni Jun 09 '22 at 13:13