Search code examples
pythonsubprocesspython-importpython-venvpython-packaging

Python Subprocess cannot find module


Been stuck on this for a while and can't quite work it out.

Setup

Folder Structure

- Home
  - folder1
    + script1.py
    + script2.py
  - folder2
    + script3.py

Scripts

Script1

import logging
logger=........

import os
cwd = os.getcwd()

import sys
print(sys.executable)

subprocess.run([sys.executable, 'folder1/script2.py'], cwd=cwd)

Script2

import logging

logger=........

try:
    from folder2 import script3
except Exception as e:
    logger.exception(f'Failed to open: {str(e)}')

Script3

import logging
logger=........

<<VariousFunctions>>

I'm within a virtual environment and use spyder.

Issue

script2 and script3 run fine, the default working directory is the project home, Home. However, I cannot get script2 to find script3 using a subprocess. I have ensured it is using the correct interpreter sys.executable and set the working directory to be the same too via os.getcwd(). I put these in the log and as far as I can see everything is set correctly but can't seem to import the module.

ModuleNotFoundError: No module named 'folder2' 

What am I missing ?


Solution

  • Try this

    import sys
    sys.path.append(".")
    from parent_dir.folder import module
    

    According to the docs you are appending the current path to PYTHONPATH and you should then be able to access modules in the path. You can specify sys.path.append("..") depending on what directory you want to add to the PATH