Search code examples
pythonpyqt5python-poetry

python import not recognized of my internal script


I have a pyqt app that runs scripts dynamically. I execute those scripts by using

subprocess.Popen(['python', 'script1.py', 'arg1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

I am able to reach script1.py but I am getting the error:

  from utils import file_utils\r\nModuleNotFoundError: No module named \'utils\'\r\n'

This is the import from the file being run dynamically:

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../utils')))
from utils import file_utils

file_utls.py is just a script. Not a module or class or anything.

When I run the app direcrly using python interpreter, everything looks good. However, I run it after using

pyinstaller --onefile pyqt.pyw -i pyqt.ico

and clicking on the exe file being created. It is worth mentioning that when I am not using that import, such as with other scripts, everything runs smoothly. Google Bard suggested I should use absolute path or use os.path, like I have, So, how can I resolve that import?

Edit: directory structure:

  • folder-root
  • pyqtWindowApp.py - the main app that loads the script
    • actionables
      • python
        • file_to_run.py
    • utils
      • file_utils.py

Also i have added prints to the paths of the file to be executed

if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
    print('frozen')
    print (application_path)
else:
    application_path = os.path.dirname(os.path.abspath(__file__))
    print ('not frozen')
    print(application_path)

sys.path.append(os.path.join(application_path, '../../utils'))
import file_utils

This prints:

path-to-project\actionables\python

Update:

I've reduced the problem to an issue with poetry.

When I run this app using my PyCharm, everything works smoothly. When I run it from inside poetry shell, the same error happens.

I've added the following in pyproject.toml

Notice: 'utils' folder has changed to 'python_utils'

[tool.poetry.scripts]
scripts = [
    'python_utils\file_utils.py'
]

when I run poetry install I get The Poetry configuration is invalid:

  • [scripts.scripts] ['python_utils\file_utils.py'] is not valid under any of the given schemas

Solution

  • Your python_utils module is not a 3rd party, thus it's not included in the site-packages directory, which is why the python interpreter can't locate it.

    In order to add your files, you should add your codebase to the PYTHONPATH environment variable using the optional env argument of the Popen function:

    new_env = os.environ
    new_env['PYTHONPATH'] = new_env.get('PYTHONPATH','') + ";" + os.getcwd() # assuming you set your working directory to the codebase
    subprocess.Popen(['python', 'script1.py', 'arg1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=new_env)