Subprocess inherits sys.path from application packed with PyInstaller

23 views
Skip to first unread message

Spooky Shadow

unread,
May 11, 2020, 4:16:36 AM5/11/20
to PyInstaller

I have a tool organizer written in python that opens other apps using subprocess. Those apps can execute scripts written in python. The problem is that when I pack organizer with PyInstaller and run it, whenever apps oppened by it run python scripts, they use python packed inside of organizer.

Easiest example: I open cmd via organizer and write this:

>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Users\\myuser\\AppData\\Local\\Temp\\_MEI156362\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\lib\\site-packages\\wx-3.0-msw']

If I open cmd regulary and do the same the output is like this:

['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\lib\\site-packages\\wx-3.0-msw']

How can i make it use the sysytem interpreter when oppened from .exe?

Chris Barker

unread,
May 12, 2020, 3:15:11 AM5/12/20
to pyins...@googlegroups.com
On Mon, May 11, 2020 at 1:16 AM Spooky Shadow <demon...@gmail.com> wrote:
 

I have a tool organizer written in python that opens other apps using subprocess.


This is going to to come down to the details of how exactly you do that. I'm a little fuzzy, but the subprocess module allows you to control how things are set up before you run the command. in this case, you may be interested in:

"""
If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly to Popen.
"""
and maybe the "shell" parameter as well.

If you are simply using "python.exe", then it'll be up to the whatever PATH is set to find Python.

the trick here is that you are shipping one thing, and then expecting it to run stuff with "the system Python", but Windows does not HAVE a system Python, so that may not exist, or be installed in a different way, etc. If you can count on your users to have installed the cPyton installed in the degfault way, then you can probably use a full path, somethign like:

C:\\WINDOWS\\SYSTEM32\\python27]python.exe 

(not on Windows now, so not exactly sure).

However:

1) maybe you DO want the subprocesses to use your bundled Python? tHat way you can be assured that it's bundled hwo you want it, with any required dependencies, etc.

2) your sys.path is pretty odd -- it seems to be using the bundled python, but it's got a bunch of system stuff on the sys.path -- THAT seems a recipe for disaster.

HTH,
-CHB










 
--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/d42c6742-c52e-4286-911d-9521e41aaedc%40googlegroups.com.


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

bwoodsend

unread,
May 12, 2020, 7:58:11 AM5/12/20
to PyInstaller
I'm not sure if this is relevant or not but `sys.path` is the PATH environment variable. It's PYTHONPATH plus some internal folders of whichever python you are running. `sys.path` says where to search for modules whenever it encounters an `import` statement. It will not affect subprocess calls of any sort. To get PATH use `os.environ["PATH"]`.
Reply all
Reply to author
Forward
0 new messages