1

I am creating a Python package and want to make the example files in the example folder runnable with something like python example_file.py. How do I do it? My example_file.py and __init__.py look like below:

# example_file.py
from demandforecast import DemandForecast # module coded in demandforecast.py

if __name__ == '__main__':
    # Example code here
# __init__.py
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

When I navigate to the examples directory and run python example_file.py I get the following error:

Exception has occurred: ModuleNotFoundError
No module named 'demandforecast'

This can be solved by adding using the old sys.path.insert() trick mentioned in Importing files from different folder, but I would rather not include the absolute path because this will vary from user to user.

Here is my directory structure:

├───demandforecast
│       demandforecast.py
│       __init__.py
│
└───examples
        example_file.py

I have tried a few posts here, such as Relative imports in Python 3, but without luck. Adding sys.path.append('../') to the example file before the demandforecast import also did not work.

  • `import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))`: better to not do that: an unsuspecting user will not know you temporarily altered the path. – 9769953 Feb 09 '22 at 14:43
  • 1
    Why not run your examples like `python examples/example_file.py`? Or `cd examples; PYTHONPATH=.. python example_file.py`. Or, once the package is properly installed, `cd examples; python example_file.py` without the `PYTHONPATH` PART. – 9769953 Feb 09 '22 at 14:44
  • Keep in mind: the examples should really only be run once the package is installed, so the path should be of no concern. Whether the user wants to change into the directory and run the example, or run them from the root directory, is up to the user. – 9769953 Feb 09 '22 at 14:46
  • I've removed the line from __init__.py. Still, running `python examples/example_file.py` gives me the same error. I'm doing this to debug the example file (and potentially the module) without installing the package, so I'll just move the example to the root folder. Thanks, though! – Bernardo Trindade Feb 09 '22 at 14:53

2 Answers2

0
from src.pro.demandforecast.demandforecast import foo

if __name__ == '__main__':
    foo()

output:

foo

create a folder src and inside of it create a package pro and now inside pro create two packages demandforecast and examples.

Directory structure:

/src$ tree
.
└── pro
    ├── demandforecast
    │   ├── demandforecast.py
    │   ├── __init__.py
    │   └── __pycache__
    │       ├── demandforecast.cpython-38.pyc
    │       └── __init__.cpython-38.pyc
    ├── examples
    │   ├── example_file.py
    │   └── __init__.py
    ├── __init__.py
    └── __pycache__
        └── __init__.cpython-38.pyc

IDE:

PyCharm

Udesh
  • 2,415
  • 2
  • 22
  • 32
0

I think your import is simply incorrect, and should be:

from demandforecast.demandforecast import DemandForecast

Since demandforecast is both the package name (the directory is definitely a package, with the __init__.py file) and a module name within that package. (I assume DemandForecast is a class within the demandforecast module.)

9769953
  • 10,344
  • 3
  • 26
  • 37