0

I have a python script main.py which imports various modules from other scripts in the same directory.

With the lines

import sys
print(sys.path)

I can see that when I run main.py, the directory it is in (binBuild below) is added to the PYTHONPATH along with the python version I am using (python3.5 from miniconda)

['/ms/data5/USERNAME/Programmes/MYCODE/binBuild', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python35.zip', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/plat-linux', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/lib-dynload', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/site-packages', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg']

main.py contains two options - it can run directly, or, with a flag specified it can call a gui. At this stage main.py runs with no errors.The gui contains various options that can be set and then when processing is started calls back to main.py to start the main run of the code. At this point the code fails with the following error:

Traceback (most recent call last):
  File "/ms/data5/USERNAME/Programmes/MYCODE/binBuild/GUI/../main.py", line 12, in <module>
    from GlobalParams import GlobalParams
ModuleNotFoundError: No module named 'GlobalParams'

If I print the PYTHONPATH at this stage I can see that the PYTHONPATH now contains two copies of the path to the directory containing main.py.

['/ms/data5/USERNAME/Programmes/MYCODE/binBuild', '/ms/data5/USERNAME/Programmes/MYCODE/binBuild', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python35.zip', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/plat-linux', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/lib-dynload', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/site-packages', '/ms/data5/USERNAME/Programmes/MYCODE/miniconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg', '/ms/data5/USERNAME/anaconda3/lib/python37.zip', '/ms/data5/USERNAME/anaconda3/lib/python3.7', '/ms/data5/USERNAME/anaconda3/lib/python3.7/lib-dynload', '/ms/data5/USERNAME/anaconda3/lib/python3.7/site-packages']

I also notice at this stage it picks up another copy of python (3.7) which is installed elsewhere on the system.

However, python is no longer able to import any of the modules from the binBuild directory (where main.py is present). Why can python no longer find modules even though they are in a directory on its path. Is this expected behaviour?

218
  • 1,754
  • 7
  • 27
  • 38

1 Answers1

0

After some more debugging I discovered the problem, so I'm just posting what helped me here in case this is useful for others. The clue was in the appearance of the extra python distribution (/ms/data5/USERNAME/anaconda3/lib/python3.7/) in the PYTHONPATH. Due to a bug in the code the GUI wasn't calling the packaged python (python3.5) but the system python.

I detected this by putting the following lines in main.py

import sys
print(sys.version_info)

This showed that the python version was changing between running main.py to open the GUI and the GUI calling back to main.py. The following stack overflow answer was useful in this context:

How do I detect the Python version at runtime?

218
  • 1,754
  • 7
  • 27
  • 38