Embeddable distribution
If there is anything I like about Windows as a pythonist, it must be that you can use embedded distribution of python.
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.
In my opinion, it is a portable, ready-to-ship virtualenv. However, the embedded distribution comes with some limitation:
Third-party packages should be installed by the application installer alongside the embedded distribution. Using pip to manage dependencies as for a regular Python installation is not supported with this distribution, though with some care it may be possible to include and use pip for automatic updates. In general, third-party packages should be treated as part of the application (“vendoring”) so that the developer can ensure compatibility with newer versions before providing updates to users.
Sounds scary right? It said it doesn't even support pip. Don't worry, followthese simple steps, you will have a fully workable embedded environment.
Get the distribution
- Go to https://www.python.org/downloads/windows/
- Choose the version python you like and download the corresponding
Windows x86-64 embeddable zip file
. - Unzip the file.
To make this tutorial easy to follow, I am assuming that you have downloaded Python3.7
and unzipped it toC:\python\
.
Get pip
The distribution does not have pip installed in place, you need to install it yourself:1. Download get-pip.py
at https://bootstrap.pypa.io/get-pip.py2. Save it to c:\python\get-pip.py
3. In command-line run C:\python\python get-pip.py
4. pip is now installed
Config path
The runtime of this distribution doesn't have an empty string ''
added in sys.path
,so that the current directory
is not added into sys.path
, to solve the problem,you need to:
- Open
C:\python\python37._pth
. - Uncomment the line
#import site
and save. - Create a new .py file and save it as
c:\python\sitecustomize.py
:
import sys
sys.path.insert(0, '')
lib2to3 issue
You will encounter the following error when you try to install some packages:
error: [Errno 0] Error: 'lib2to3\\Grammar3.6.5.final.0.pickle'
- Unzip
C:\python\python37.zip
to anew folder
- Delete
C:\python\python37.zip
- Rename the
new folder
topython37.zip
(yes, a new folder calledpython37.zip
)
Python's import module is able to treat zip file as folder however, it cannot read pickle file inside a zip file, so unzip it and rename it.
Running pip
If you don't want to mess with you PATH, you can simply do the following in your window command prompt:
CD C:\python\Scripts
pip install xxxxx
Running Scripts
Again if you don't want to mess with you PATH, you can simply do in your window command prompt:
C:\python\python <path to your script>
Top comments (4)
Great info...
I'm fiddling with that 'embbeded' incarnation too... (v 3.8)
Do you know where to change the configuration
(maybe a configuration file... or registry in Windows???)
so that we can extract python37.zip to a folder ( Lib is an idea ;-) )
(and delete the zip file)
So python will work as usual???
just signed up for an account on DEV to like your article .... Thanks for the valuable information
thanks for sharing, awesome post crack!
Awesome info!