Windows setup

Setting up Python on Windows, including isolated virtual environments. Includes optional instructions for:

IDE only

It is probably easiest to simply:

Alternatives to PyCharm include lighter-weight environments like Geany, and other feature-full IDEs like Ninja-IDE. See the Python Guide for a well-maintained list of options (link).

Command line tools

If you like, or suspect you would like, some command line options in addition to PyCharm, you can install a few extra tools...

Install Python and pew

(Assumes you are starting from scratch.)

python

exit() #this is a Python command

pip install pew
#this installs pew as a system-level tool
#usually we only use pip in a virtualenv (see below)

You are now ready to use Python and virtual environments from the command line. When reading instructions on using Python on other pages, note that you have access to the following commands and don't need to reinstall them:

Using command line tools

Just for fun...

If you want, you can now install youtube-dl, a tool to download videos for offline viewing. From the command line:

pip install youtube-dl
#this installs youtube-dl as a system-level tool

You can now invoke youtube-dl at any time from the command line to download videos from Vimeo, YouTube and other video sites:

youtube-dl <SOME_VIDEO_URL>

For more information, see http://youtube-dl.org (you do not have to download the exe).

Making a new virtualenv

Before starting a project, you should create an isolated environment for it. This is called a virtualenv. To create one run:

pew new <SOME_NAME>

Once the command is complete (it might take a while), the prompt will change and you will be "inside" a new virtual environment called SOME_NAME. You can install Python packages (libraries) in the current virtualenv, independently of other virtualenvs, for example:

pew new test
# creates virtualenv named "test" and opens it
pip install BeautifulSoup4
#installs the "BeautifulSoup4" package in the "test" virtualenv

You can see all your currently installed libraries by running

pip freeze
#returns a list of packages installed in the current virtualenv

You will now have access to the BeautifulSoup library when you run:

python <FILENAME>.py

To leave the virtualenv, type:

exit #the prompt will change to indicate you have left

Outside the virtualenv, running the .py file will fail, because the packages won't be accessible. To return to an existing virtualenv, type:

pew workon <SOME_NAME>

...and the script will work again.

Using both pew and PyCharm

Additional pip/pew commands

The following commands will be useful. (For a full list see the official Usage and Command Reference documentation.)

To list all virtualenvs:

pew ls

To show the current virtualenv:

pew show

(if the result is "pew-show [env]" then you are not currently in a virtualenv, otherwise it will return the name of the current virtualenv)

To upgrade a package:

pip install -U <PACKAGE-NAME>

To uninstall a package (for example, if you accidentally install it on the system rather than in a virtualenv):

pip uninstall <PACKAGE-NAME>

Python 2

You may need to install Python 2 alongside Python 3, for example when supporting legacy software or libraries. Set it up as follows:

Install Python 2

python

Note that Python 2 is now your default when typing python at the command line. This is normal behaviour for side-by-side installations. See below for instructions on invoking Python 3.

How to invoke Python 2 or Python 3

Because python now always invokes Python 2, we need a new way to specify which version of Python we want. (This command was installed when we installed Python 3, but we haven't needed it until now.)

To invoke Python 2, run one of the following:

python <SCRIPT>.py
py <SCRIPT>.py
py -2 <SCRIPT>.py

To invoke Python 3, run the following:

py -3 <SCRIPT>.py

Alternatively, if you set the hashbang line for a script, you can invoke it with just:

py <SCRIPT>.py

and the correct version will be used. For details, see https://docs.python.org/3.4/using/windows.html#from-a-script

Making a new Python 2 virtualenv

pew will still create Python 3 virtualenvs by default (because we installed it under Python 3). To create a virtualenv based on Python 2:

pew new -p C:\Python27\python.exe <SOME_NAME>
#(assumes you installed Python 2.7.x to default location)
#creates a new virtualenv based on Python 2
#with pip and setuptools already installed

Install pip for Python 2 (only if required)

You should not need pip for Python 2 unless you are installing system-level packages in Python 2. Typically, you do not need to do this and would use a Python 2 virtualenv instead (see above). If you do need to install

py -2 get-pip.py

pip2 --help #should return help for pip
pip --version #should return pip version number and reference Python 2 interpreter

Note that pip now runs in Python 2 by default. This is normal behaviour for side-by-side installations.

Potential error: pip2 won't run / incorrect Python version

This problem is only relevant if you are trying to install pip for Python 2. If the test doesn't work, it probably means your PATH has not been updated correctly (in Python 2.7.7, the Scripts path will not be added to your PATH variable). Check your path and update it so that it is as follows:

C:\Python27\;C:\Python34\;C:\Python27\Scripts;C:\Python34\Scripts;<REST_OF_YOUR_PATH>
#(assumes you have installed Python 2.7.x and Python 3.4.x to the default locations)

Then restart your machine, and check that Python 2 is in use when running both python --version and pip --version

How to invoke pip for Python 2 or Python 3 (only if required)

If you installed pip as above, the rules for invoking it, including the default behaviour, have now changed. To invoke pip for Python 2, run one of the following:

pip <COMMAND>
pip2 <COMMAND>

To invoke pip for Python 3, run the following:

pip3 <COMMAND>

References and further reading

Windows installation according to the Python Guide
http://docs.python-guide.org/en/latest/starting/install/win/
(recommends Python 2.7 and a different set of virtualenv tools)

Installation (pip)
https://pip.pypa.io/en/latest/installing.html
(confirms that pip now installs setuptools by default)

PEP 397 -- Python launcher for Windows
http://legacy.python.org/dev/peps/pep-0397/
(explains logic and implementation of the "py" command)
TODO: reference py rather than python command above?



Backlinks: python python:setuptools python:pew python:pip

CC0 / Public domain dedication To the extent possible under law, d3vid seaward has waived all copyright and related or neighboring rights to "Windows setup in Grasmere notebook, including code snippets" (why? how?)