Python: Install Python with pip on Windows by the embeddable zip file

Hi,

to install Python on Windows download the latest version. In this example Python 3.6.5.

Extract the zip file to an directory, e.g. D:\python3.6.5.

To install pip download the latest version of get-pip to the pythons install path and start installation.

d:\> cd /d D:\Python3.6.5
D:\Python3.6.5> python get-pip.py
...
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-10.0.1 setuptools-39.2.0 wheel-0.31.1

If you are behind a proxy add the –proxy switch

D:\Python3.6.5> python get-pip.py --proxy="http://192.168.254.1:8888"

Unfortunately in the default configuration you can not load any module installed by pip, pip itself too. Because the sys.path variable just contains Python Zip file and the path to the python directory where the python executable is located.

>>> import sys
>>> print(sys.path)
['D:\\Python3.6.5\\python36.zip', 'D:\\Python3.6.5']
>>> import pip
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pip'

Any try to expand the variable by setting a PYTHONPATH variable are ignored. Root cause is that the embeddable zip file installation package contains a file python36._pth which overwrites all other possibilities to set the sys.path variable. sys.path contains all directories where python looks for modules.

To set the sys.path variable open the _pth file an add the following pathes at the and of the file. Replace “D:\Python3.6.5” with your installation directory.

D:\Python3.6.5
D:\Python3.6.5\DLLs
D:\Python3.6.5\lib
D:\Python3.6.5\lib\plat-win
D:\Python3.6.5\lib\site-packages

Or rename the python36._pth file

D:\Python3.6.5> ren python36._pth python36._pth.save

and set the PYTHONPATH environment variable for the current user.

setx PYTHONPATH "D:\Python3.6.5;D:\Python3.6.5\DLLs;D:\Python3.6.5\lib;D:\Python3.6.5\lib\plat-win;D:\Python3.6.5\lib\site-packages"

or for the whole system

setx /M PYTHONPATH "D:\Python3.6.5;D:\Python3.6.5\DLLs;D:\Python3.6.5\lib;D:\Python3.6.5\lib\plat-win;D:\Python3.6.5\lib\site-packages"

That’s it 🙂

Michael

Advertisment to support michlstechblog.info

15 thoughts on “Python: Install Python with pip on Windows by the embeddable zip file”

  1. Thank you for the instructions, it worked for me.

    Have you tried “virtualenv” or “python -m venv”?

    I got:

    ———–for virtualenv
    D:\projects\test>virtualenv ENV
    Using base prefix ‘d:\\python3.7-embed’
    Traceback (most recent call last):
    File “runpy.py”, line 193, in _run_module_as_main
    File “runpy.py”, line 85, in _run_code
    File “D:\ython3.7-embed\Scripts\virtualenv.exe\__main__.py”, line 9, in
    File “D:\ython3.7-embed\lib\site-packages\virtualenv.py”, line 712, in main
    symlink=options.symlink)
    File “D:\ython3.7-embed\lib\site-packages\virtualenv.py”, line 927, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
    File “D:\ython3.7-embed\lib\site-packages\virtualenv.py”, line 1149, in install_python
    writefile(site_filename_dst, SITE_PY)
    File “D:\ython3.7-embed\lib\site-packages\virtualenv.py”, line 363, in writefile
    with open(dest, ‘wb’) as f:
    FileNotFoundError: [Errno 2] No such file or directory: ‘D:\\projects\\test\\ENV\\python37.zip\\site.py’
    ———–for virtualenv end

    and

    ———–for venv
    D:\projects\test>python -m venv VENV
    D:\ython3.7-embed\python.exe: No module named venv
    ———–for venv end

  2. Looks like you can also add relative paths, e.g.
    .\Lib
    .\Lib\site-packages
    .\Scripts

    These then show up when calling print(sys.path)

  3. There’s a note in python32._pth:
    # Uncomment to run site.main() automatically
    #import site

    Then `site-packages` folder is added automatically to sys.path

  4. Hey, thanks, everything you mentioned here works for me, but when I try to import a module in the same directory, it fails.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.