-1

I've organized my project by separating different functionality, but don't how to communicate between the parts I've created.

I'm trying to access functions from run_perf.py in addMetrics.py by including this import statement

from analysis import run_perf
...
runtime = run_perf.calcMs()

Keep getting ModuleNotFoundError: No module named 'analysis' but don't know how to fix it.

Project Structure:

myApplication/
│
├── bin/
│
├── myApplication/
│   ├── __init__.py
│   ├── runner.py
│   ├── analysis/
│   │   ├── __init__.py
│   │   ├── run_perf.py
│   │   └── mem_perf.py
│   │
│   └── conversion/
│       ├── __init__.py
│       ├── convert.py
│       └── addMetrics.py
│
├── .gitignore
├── LICENSE
└── README.md

(If its worth noting, my __init__.py files are also empty at the moment)

cramos24
  • 29
  • 8

2 Answers2

0

Received a very helpful message that fixed the issue!

I’m trying to access functions from run_perf.py in addMetrics.py by
including this import statement

from analysis import run_perf
...
runtime = run_perf.calcMs()

I keep getting ModuleNotFoundError: No module named ‘analysis’ but don’t know what about my import is wrong.

That means that Python can’t fine the “analysis” package. Python looks in sys.path:

from sys import path print(repr(path))

/Users/cameron/lib/python:/Users/cameron/rc/python

which contains my personal code and tabhistory for interactive python use.

based on your file tree:

File Structure:

appName/
âââ bin/
âââ appName/
â   âââ __init__.py
â   âââ runner.py
â   âââ analysis/
â   â   âââ __init__.py
â   â   âââ run_perf.py
â   â   âââ mem_perf.py
â   âââ conversion/

[…]

You need to include /path/to/appName/appName in your $PYTHONPATH.

For testing purposes, do that by hand< example:

export PYTHONPATH=/path/to/appName/appName

For an appplication tree like the above, as installed, one typical approach would be to have the executable in appName/bin be a small wrapper script which figured that out from its own path, set $PYTHONPATH, then executed Python on your real Python main programme. Or alternatively to have the installation process wire the required path into the wrapper script to avoid having it guess.

Cheers, Cameron Simpson cs@cskk.id.au

https://discuss.python.org/t/calling-functions-from-packages/10107/2

cramos24
  • 29
  • 8
-1
from myApplication.analysis import run_perf

Note that the package name myApplication violates PEP8 rules.

Peter
  • 10,959
  • 2
  • 30
  • 47
  • Just using _myApplication_ as an example, thanks for the heads up. Your solution still resulted in the error: **ModuleNotFoundError: No module named 'myApplication'** – cramos24 Aug 11 '21 at 20:07
  • This is based on the limited of information you shared with us. I assumed you're starting from within the top folder, but obviously you don't. So where is your top level script located and did you properly install your module in editable mode? – Peter Aug 12 '21 at 06:36