Can't run python script from terminal but can from PyCharm: ModuleNotFoundError

OK, thanks.

So having set my PYTHONPATH, and included

for index, path in enumerate(sys.path):
print(f’{index}. {path}')

at the top of MAX1_Controller.py, I get the following output:

  1. /data/stuart/Projects/Python/BackTesting/MAX1
  2. /data/stuart/Projects/Python/BackTesting/MAX1
  3. /data/stuart/Projects/Python/BackTesting/BackTestPkg
  4. /data/stuart/Projects/Python/BackTesting/TradingPkg
  5. /usr/lib/python312.zip
  6. /usr/lib/python3.12
  7. /usr/lib/python3.12/lib-dynload
  8. /data/stuart/Projects/Python/Env/lib/python3.12/site-packages
    Traceback (most recent call last):
    File “/data/stuart/Projects/Python/BackTesting/MAX1/MAX1_Controller.py”, line 15, in
    import MAX1_Main as main
    File “/data/stuart/Projects/Python/BackTesting/MAX1/MAX1_Main.py”, line 8, in
    from TradingPkg import PlatformLib as pl
    ModuleNotFoundError: No module named ‘TradingPkg’

This suggests to me that the paths are correct regardless of how I created them, but it is still not allowing my script to run.

What am I missing?

Should look like this:

0. 'whatever current working directory is' or blank
1. /usr/lib/python312.zip
2. /usr/lib/python3.12
3. /usr/lib/python3.12/lib-dynload
4. /data/stuart/Projects/Python/Env/lib/python3.12/site-packages
5. /data/stuart/Projects/Python/my_directories

The files/modules in both packages should be in the my_directories folder and not the actual folders for simplicity.

Here is another thread for adding directories to PYTHONPATH if that helps:

As a newbie, your comments prompt me to ask a couple of questions:

  1. Why not test it in my application. I have commented out the important code
  2. As I have not yet moved my packages into a common folder ‘my_packages’ why should the 2 packages not be in their own directories?

I am going to play with the common packages folder over the weekend, and point PYTHONPATH to the common folder. Should this common folder have a init.py file?

This is gerally the way you develop code (also done in hardware, i.e., circuits, etc.) Otherwise, generally speaking (ahem, chatting), it will be much harder to debug a program since you will be combining different pieces that have not yet been proven to work - where would you start?. That is the general rule of thumb. You first have to verify and test before integrating into a much larger system. You will see that this approach pays dividends as you gain more experience.

I suppose that you can. But it makes the PYTHONPATH less cluttery. Suppose you have ten different library modules. Do you really want to add 10 additional paths to PYTHONPATH for every module added? It is just much simpler to add one directory and include them there. If you look into the Lib path, and open it, and click on any library package (say, tkinter or asyncio) you will see that they have multiple modules not just one. That is, you group similar library modules together.

This was a requirement before but no longer.

Paul,
Thanks for all your help, I have now resolved the problem. In the end it was something simple - I had set my PYTHONPATH environment variable to be the same as each of my packages, when it really needed to be the parent folder. Works a treat now.
Regards, Stuart

1 Like