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

前端 未结 2 892
夕颜
夕颜 2020-12-18 04:45

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) Win

相关标签:
2条回答
  • 2020-12-18 05:27

    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

    0 讨论(0)
  • 2020-12-18 05:35

    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.

    0 讨论(0)
提交回复
热议问题