michi.wtr
Goto Top

Use modules from venv in a started Process

Hey Guys,

I started to develop a small application in python. Unfortunally I have absolutely no expirience with python.
But as I started I got impressed by the module "venv" to create a virtual module, so I can install my dependend modules there and don't change my actual environment.

Now I have to start another python process inside the program, but using my python.exe I get the error, that python don't know my modules. (they are installed in the virtual environment)

I want to put the python.exe inside my own executable application, so the app is working on each device, unless python interpreter is installed or not.
I want to start another python script in my code:
self.__topic[topic] = Popen(
            # runs the executable HTMLViewer.exe with the path of the HTML file and encoding
            ["assets/executables/python.exe", resourcePath("assets/modules/HTMLViewer.py"), file, encoding]  
        )
This should run the script HTMLViewer.py with filepath and encoding as parameters with my python.exe which i copied into the ./assets/executables/ folder.

Problem is, that while debugging, the python.exe gets started, but has a problem with the HTMLViewer.py, because it imports modules which are installed in the venv.
But even when compiling the whole script into a executable with
pyinstaller --onefile --windowed client.spec
(client.spec includes all assets etc. i need to run) the HTMLViewer.py is not started correctly.

What can I do, to use the modules used in my venv in the started python.exe or why does it not work after I compile to a executable (modules should be included?)

Thanks for your help in advance,
Micha

Content-ID: 33515909516

Url: https://administrator.de/contentid/33515909516

Ausgedruckt am: 24.11.2024 um 10:11 Uhr

michi.wtr
michi.wtr 07.08.2024 um 10:30:25 Uhr
Goto Top
Is it possible to pass modules to the python.exe (or better pythonw.exe) ?
I thought about it, and even if i make a executable containing all dependencies (modules), I also should not be able to start python.exe on a computer where it is installed in C:\Python312, because the modules are not installed in the environment. Can i pass the required modules as parameters to the python.exe?
13910172396
Lösung 13910172396 07.08.2024 aktualisiert um 11:05:15 Uhr
Goto Top
This should answer your question
https://docs.python.org/2/tutorial/modules.html#
When a module named "spam" is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named "spam.py" in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

I want to put the python.exe inside my own executable application, so the app is working on each device, unless python interpreter is installed or not.
But this will only work if the used python exe is compiled against the platform where it is executed.

Regards.
michi.wtr
michi.wtr 07.08.2024 um 11:09:37 Uhr
Goto Top
Quote from @13910172396:

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

does this mean i have to copy the modules out of my venv into my project path, then put them into the executable and modify the sys.path, so it includes the paths for the modules?
13910172396
13910172396 07.08.2024 um 11:12:49 Uhr
Goto Top
Yes.
michi.wtr
michi.wtr 07.08.2024 um 15:16:21 Uhr
Goto Top
I think it is the right way, but I am not able to get it done...

with open("output.txt", "w") as a:  
            a.write(str(sys.path) + "\n")  
        self.__topic[topic] = Popen(
            [
                "assets/executables/pythonw.exe",  
                resourcePath("assets/modules/HTMLViewer.py"),  
                file,
                encoding,
            ]
            + sys.path
        )
import sys

a = open("output.txt", "a")  
a.write(str(sys.path) + "\n")  
a.write(str(sys.argv) + "\n")  

if len(sys.argv) > 3:
    sys.path = sys.argv[3:]

from PyQt5.QtCore import Qt, QTimer, QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget

a.write(str(sys.path))
a.close()

While debugging the file <output.txt> has the following content:
Skript 1 (sys.path):
['c:\\Users\\User Name\\Projects\\alert', 'C:\\Python312\\python312.zip', 'C:\\Python312\\DLLs', 'C:\\Python312\\Lib', 'C:\\Python312', 'c:\\Users\\User Name\\Projects\\alert\\venv', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages\\setuptools\\_vendor']  

Skript 2 (sys.path):
['C:\\Users\\User Name\\Projects\\alert\\assets\\modules', 'c:\\Program Files\\VS Code\\data\\extensions\\ms-python.debugpy-2024.10.0-win32-x64\\bundled\\libs\\debugpy\\_vendored\\pydevd', 'C:\\Python312\\python312.zip', 'C:\\Python312\\DLLs', 'C:\\Python312\\Lib', 'C:\\Users\\User Name\\Projects\\alert\\assets\\executables', 'C:\\Python312', 'C:\\Python312\\Lib\\site-packages']  

Skript 2 (sys.argv):
['C:/Users/User Name/Projects/alert/assets/modules/HTMLViewer.py', 'C:/Users/User Name/Projects/alert/assets/sites/fire.html', 'UTF-8', 'c:\\Users\\User Name\\Projects\\alert', 'C:\\Python312\\python312.zip', 'C:\\Python312\\DLLs', 'C:\\Python312\\Lib', 'C:\\Python312', 'c:\\Users\\User Name\\Projects\\alert\\venv', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages\\setuptools\\_vendor']  

Skript 2 (sys.argv):
['c:\\Users\\User Name\\Projects\\alert', 'C:\\Python312\\python312.zip', 'C:\\Python312\\DLLs', 'C:\\Python312\\Lib', 'C:\\Python312', 'c:\\Users\\User Name\\Projects\\alert\\venv', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages', 'c:\\Users\\User Name\\Projects\\alert\\venv\\Lib\\site-packages\\setuptools\\_vendor']  
all works perfectly fine...


After compiling the script as executable, the file <output.txt> has this content:
USERl WalterSkript 1 (sys.path):
['C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\base_library.zip', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\lib-dynload', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\setuptools\\_vendor']  

Skript 2 (sys.path):
['C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\assets\\modules', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\python312.zip', 'C:\\Python312\\Lib', 'C:\\Python312\\DLLs', 'C:\\Users\\User Name\\Projects\\alert\\dist', 'C:\\Users\\User Name\\Projects\\alert\\dist\\Lib', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\assets\\executables']  

Skript 2 (sys.argv):
['C:/Users/USER~1/AppData/Local/Temp/_MEI121162/assets/modules/HTMLViewer.py', 'C:/Users/USER~1/AppData/Local/Temp/_MEI121162/assets/sites/fire.html', 'UTF-8', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\base_library.zip', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\lib-dynload', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162', 'C:\\Users\\USER~1\\AppData\\Local\\Temp\\_MEI121162\\setuptools\\_vendor']  

Skript 2 (sys.path):
The sys.path is nothing and the code does not work as expected....