Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Embeddable Zip File Doesn't Include lib/site-packages in sys.path

I am attempting to update our Python version from 3.4 to 3.6. We are embedding Python into a C++ application, so it seemed logical to utilize the new (since Python 3.5) Windows x86 embeddable zip file. However, our application is failing to execute because "lib/site-packages" isn't being added to the sys.path variable. I have confirmed that after installing Python 3.6 on my machine, and running from the installed location, the sys.path variable contains (relative to the python directory):

'...\\python36.zip'
'...\\DLLs'
'...\\lib'
'...'
'...\\lib\\site-packages'

However, when I run from the embeddable zip file on the same machine, the sys.path variable contains (relative to the python directory):

'...\\python36.zip'
'...'
'...\\\n'

In both cases, the "lib/site-packages" directory exists within the Python directory. I also don't have a PYTHONPATH environment variable defined. Does anyone know how to get the embeddable zip file to act the same as the installed version, with respect to how it determines sys.path?

like image 991
Jeff G Avatar asked Jun 08 '17 18:06

Jeff G


People also ask

How do I use Python Windows x86 64 embeddable zip file?

Installing PythonDownload the embeddable zip file from the link https://www.python.org/downloads/release/python-374/. Extract the archive or zip file into a physical drive location. Let's say under C drive. So the extracted python root directory is C:\python-3.7.

What is Python embeddable zip file?

The embeddable package. New in version 3.5. The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.


2 Answers

After extracting the Python embeddable zip file, there is a file called python36._pth in the root directory. That file contains the following text:

# Uncomment to run site.main() automatically
#import site

As the comment indicates, simply uncomment the import site statement by removing the '#' character. After doing so, the sys.path variable contains:

'...\\python36.zip'
'...'
'...\\\n'
'...\\lib\\site-packages'

This is still different than the installed version, but is exactly what I needed in my particular case.

BEGIN EDIT

I also discovered that you can remove the python36._pth file entirely, which reverts Python to behavior of the non-embeddable version.

like image 192
Jeff G Avatar answered Sep 26 '22 02:09

Jeff G


The previous answers didn't work for us, we had to modify the ._pth file to directly add the site-packages folder to sys.path. We're working with python 3.8 in a windows embeddable python environment.

So our python38._pth file looks like this now

python38.zip
.
python38.zip\\site-packages

#Uncomment to run site.main() automatically
import site

To be clear, we ran python initially with just 'import site' uncommented, all we saw in sys.path was the python38.zip and python folder paths, no site-packages at all.

So to all who may venture here, try adding the paths you want explicitly if uncommenting the 'import site' line does not work for you.

Also tried the following in our ._pth file

python38.zip/site-packages

and this also worked on windows, the '/' was correctly substituted for '\' automagically and site-packages was still in sys path and usable

like image 37
jeducious Avatar answered Sep 23 '22 02:09

jeducious