50

I installed embedded python from here, titled "Windows x86-64 embeddable zip file", but it does not have pip installed, it does not have site-packages either, when I try to do python get-pip.py it failed to run because this file has import pip in it. So how can I install pip within a embedded python environment.

Traceback (most recent call last): File ".\getpip.py", line 20061, in main() File ".\getpip.py", line 194, in main bootstrap(tmpdir=tmpdir) File ".\getpip.py", line 82, in bootstrap import pip File "", line 961, in _find_and_load File "", line 950, in _find_and_load_unlocked File "", line 646, in _load_unlocked File "", line 616, in _load_backward_compatible

The directory structure is:

Directory: C:\Downloads\python-3.6.1rc1-embed-win32

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         3/4/2017   7:26 PM         157344 pyexpat.pyd
-a----         3/4/2017   7:26 PM          97952 python.exe
-a----         3/4/2017   7:26 PM          58016 python3.dll
-a----         3/4/2017   7:26 PM        3263648 python36.dll
-a----         3/4/2017   7:26 PM        2209284 python36.zip
-a----         3/4/2017   7:26 PM             79 python36._pth
-a----         3/4/2017   7:26 PM          96416 pythonw.exe
-a----         3/4/2017   7:26 PM          23200 select.pyd
-a----         3/4/2017   7:26 PM         866464 sqlite3.dll
-a----         3/4/2017   7:26 PM         895648 unicodedata.pyd
-a----         3/4/2017   7:26 PM          83784 vcruntime140.dll
-a----         3/4/2017   7:26 PM          24224 winsound.pyd
-a----         3/4/2017   7:26 PM          45216 _asyncio.pyd
-a----         3/4/2017   7:26 PM          77984 _bz2.pyd
-a----         3/4/2017   7:26 PM         101536 _ctypes.pyd
-a----         3/4/2017   7:26 PM         215712 _decimal.pyd
-a----         3/4/2017   7:26 PM         156832 _elementtree.pyd
-a----         3/4/2017   7:26 PM        1042592 _hashlib.pyd
-a----         3/4/2017   7:26 PM         183456 _lzma.pyd
-a----         3/4/2017   7:26 PM          32416 _msi.pyd
-a----         3/4/2017   7:26 PM          25760 _multiprocessing.pyd
-a----         3/4/2017   7:26 PM          33952 _overlapped.pyd
-a----         3/4/2017   7:26 PM          61600 _socket.pyd
-a----         3/4/2017   7:26 PM          64160 _sqlite3.pyd
-a----         3/4/2017   7:26 PM        1458848 _ssl.pyd
5
  • get-pip is able to install pip even if pip isn't installed. Are you sure you're running in the conext of the embedded python? Can you show the "WHERE" that the python executable is running from?
    – nir0s
    Commented Mar 8, 2017 at 19:19
  • @nir0s where do you mean where? I run it from the folder that was unzipped with the zip file.
    – fluter
    Commented Mar 9, 2017 at 1:28
  • That's what I'm asking. Which folder? Can you print the directory contents of that folder?
    – nir0s
    Commented Mar 9, 2017 at 11:48
  • @nir0s I have updated the post with the folder contents.
    – fluter
    Commented Mar 9, 2017 at 13:02
  • Related: Adding packages to Python “embedded” installation for Windows
    – idbrii
    Commented Jul 31, 2021 at 1:59

10 Answers 10

94

Even if explicitly stated that the embeddable version of Python does not support Pip, it is possible with care. You need to:

  1. Download and unzip Python embeddable zip file.

  2. In the file python39._pth or similar, uncomment the import command. Result should look similar to this:

    python39.zip
    .
    import site
    
  3. Download get-pip.py to the Python install folder

  4. Run get-pip.py. this installs Pip into the Scripts directory:

    python get-pip.py
    
  5. Run Pip directly from command line as Pip is a executable program (this example is to install Pandas):

    .\Scripts\pip install pandas
    

You could find more information about this in the Pip issue 4207

6
  • 3
    pip install fine but then won't run properly or at all. "_pth" file needs a touch: add ".\Lib\site-packages\" below "." Commented Oct 10, 2018 at 10:01
  • 4
    and I have just found out that deleting/renaming "_pth" file frees this installation from embedded. with "_pth" file python will not recognize any other environment variable, especially PYTHONPATH. Commented Oct 10, 2018 at 10:33
  • 1
    @YılmazDurmaz Very late here... but did you come across any negative consequences wrt deleting python36._pth? Just did this, and it worked like a charm... but feels too good to be true.
    – user2849019
    Commented Dec 31, 2019 at 23:09
  • 1
    "._pth" is related to run the installation in a confined environment. deleting it frees, but still, there are problems of course, because we have only ".pyc" files for libraries, not the sources. there won't be code completion, for example, in IDEs because of this. @RTbecard Commented Jan 2, 2020 at 14:28
  • you should leave "import site" uncommented after installing pip, it will let "\lib\site-packages" in sys.path @rnso
    – tinyhare
    Commented Feb 7, 2020 at 4:33
21

how I installed pip into an embeddable python distribution:

  1. unpack the python embeddable distro archive and cd there
  2. run python ./get-pip.py (got here)
  3. EITHER just uncomment string import site inside file pythonXX._pth OR copy folders (at least pip) from the newly-created folder Lib/site-packages/ into pythonXX.zip

now it's possible to python -m pip ...

3
  • 1
    With python38-64 on Win: pip can't install any packages. ErrMsg: import distlib ModuleNotFoundError: No module named 'distlib'. distlib can be another package. Commented Apr 28, 2020 at 12:56
  • Works flawlessly with Python 3.10 win64. Commented Nov 2, 2023 at 17:21
  • Notice to make sure site-packages are picked up, one needs to run import sys; import os; sys.path.append(os.path.join(sys.executable, 'Lib', 'site-packages')) when starting python. As to how to affect it BEFORE starting Python, see issue here: bugs.python.org/issue28245 Basically, modify python3xx._pth file. Also see stackoverflow.com/a/50829079/2856597 Commented Nov 2, 2023 at 17:33
12

I recently ran into the same issue. I checked the documentation for pip and they seem to say that this use case isn't supported etc. But anyhow, here is my hack for getting the modules to work.

I installed, and by that I mean unzipped embedded python into a directory called d:\python. I assumed that the modules are going to live in that same directory.

First, to install the pip module, I needed to save the extraceted files. I changed the get-pip.py using a text editor that supported unix line terminators by removing the rmtree lines that remove the temporary and unpacked tree from the blob containted in the get-pip.py file.

I changed both locations, but only the last one was needed. The line that I changed in two locations read

shutil.rmtree(tmpdir, ignore_errors=True)

and I modified it thus (I didn't want to bother with the python indentation blocks):

print('shutil.rmtree(tmpdir, ignore_errors=True)')

I now ran my python D:\python\python.exe on the modified get-pip.py and found the temporary directory where the files were unzipped to.

I copied this directory, (check that it contains a main.py) file into the python install D:\python\pip (this is where I wanted my modules to live), ensuring that the D:\python\pip directory contained the main.py file.

The pip module is now installed in the python directory, but you need to hack pip further to remove the exception above. I did this by changing the locations.py file (in my case located in D:\python\pip\locations.py) to return the bin_py and bin_user locations of D:\python.

ie:

86 if WINDOWS:
....
bin_py = 'd:/python'
bin_user = 'd:/python'

I had to change the user_dir for pip to somewhere that would persist on this drive that was shared across multiple VMs.

The pip module now runs fine, eg

d:\python\python.exe -m pip ...

4
  • this work fine, thanks, but if i install 3-d side packages it put it into d:\python\Lib\site-packages and don't see
    – savinson
    Commented Oct 22, 2017 at 13:15
  • You might want to check the site paths.
    – user7287311
    Commented Oct 24, 2017 at 21:57
  • i cant find how to add into paths 'd:\python\Lib\site-packages' permanently otherwise it make me add this path every time i use my embedded python
    – savinson
    Commented Oct 31, 2017 at 7:40
  • 1
    Yon don't need to hardcode the location, use bin_py = os.path.dirname(sys.executable) and bin_user = os.path.dirname(sys.executable) instead.
    – Meow
    Commented Mar 21, 2018 at 4:44
11

Solution for me was deleting this file:

python39._pth

This allows Pip to work, and also allow import from same directory. Alternatively you can get this:

https://nuget.org/packages/python

Click "Download package", and you can extract just like a Zip file.

2
1

Get the PIP Standalone zip application from here: https://bootstrap.pypa.io/pip/pip.pyz

Put it anywhere on your device. I prefer to put it on one folder level above Python.exe

Then install any package with: .\python.exe .\pip.pyz install

The above method means that there is no need to download PIP, works great if you are leveraging Embedded python too. This also makes it so much more secure as you are solely installing selected packages.

Source: https://pip.pypa.io/en/stable/installation/

1

To use python embedded on winb 10 and 11 you need :

1 Download on https://www.python.org/downloads/release

2 Unzip downloaded files

3 Add Unziped python directory on path

4 In the file python39._pth uncomment import site

5 Download get-python.py script on https://bootstrap.pypa.io/get-pip.py

6 Run python get-python.py on power shell or terminal

7 Open python directory and add Script directory on path

0

download https://www.python.org/ftp/python/3.10.1/python-3.10.1-amd64.exe and install it to a Virtual box

download https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip and unpack it to python-3.10.1-embed-amd64

copy "include" & "libs" dirs from corresponding python installation "python-3.10.1-amd64.exe" from Virtual Box to python-3.10.1-embed-amd64

goto python-3.10.1-embed-amd64 folder and:

  • Download get-pip.py:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  • Execute get-pip.py:
python get-pip.py
  • rename _pth:
rename python310._pth python310._pth.renamed

set environments:

set PY_PIP=python-3.10.1-embed-amd64\Scripts
set PY_LIBS=python-3.10.1-embed-amd64\Lib;python-3.10.1-embed-amd64\Lib\site-packages

now "python -m pip install PACKETNAME" should work. PACKETNAME is tested on:

  • pywin32==302
  • psutil==5.8.0
  • pprofile==2.1.0
  • mypy==0.910
  • Pillow==8.4.0
  • cfractions==1.4.0
  • pyyaml==6.0
  • humanize==3.13.1

Note: packets installs that require to compile C/C++ are also work.

Note: 'python-3.10.1-embed-amd64' means something like c:\mypythons\python-3.10.1-embed-amd64

0

It may be considered "cheating" but I installed pycharm. It asked for the python executable, but then was able to provide pip and the packages through pip. You may need to play around a bit with pycharm, in order to create an environment around your python executable, especially if you have never used it before.

-1

Pip can also be accessed programatically within python as a command (this example installs pandas):

import pip
pip.main(['install'], 'pandas')

(Source: Installing python module within code)

1
  • I tried this approach. However it spawned a subprocess calling python, and because setuptools was not on the python path, it failed. So probably better to do one of the other approaches
    – Jon Nordby
    Commented Mar 11, 2020 at 11:48
-1

I faced the same issue and after trying multiple approaches from the existing answers, this simple solution helped me!

#My case: I installed python using embedded zip file. Set the env variables for python Now I had to install pip. So i followed cli installation

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

which created the Scripts and Lib folder under my python folder.But still i was getting, "pip is not recognized" message on cli.

#How I resolved it:

  1. Go to the python folder and in the root, you ll find a ._pth file. Open it with editor and add this line at the bottom

Lib\site-packages

So your file will look like this:

python312.zip
.

# Uncomment to run site.main() automatically
#import site
Lib\site-packages

Now save it and in your cmd, you can run pip commands, it will be successful!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.