Working with Virtual Environments

Python has a feature called virtual environments that let you set up isolated Python environments for different projects. This allows you to install the libraries that you need for one project without messing up others.

In general, you can use one virtual environment for everything in this class. However, if you'd like to separate things out for some reason, you can use multiple environments.

Creating Virtual Environments

Since Python 3.4, Python has included a virtual environment tool called pyvenv in the base Python distribution. This is automatically installed when you install Python on Windows or Mac, and the Linux instructions I provided will also result in installing it.

Using PyCharm

PyCharm can directly create a virtual environment. To do this, when you are selecting or configuring your Project Interpreter, select ‘Create Virtualenv’ from the gear dropdown. It will prompt you for a base interpreter (this should be your Python 3.4 installation) and a name & location for the virtual environment.

After you complete this form, PyCharm will automatically create a virtual environment (and, if you do this while creating a Flask application, install Flask).

After creating the virtual environment, you can go to the Project Interpreter settings and install packages by clicking the ‘+’ button at the bottom of the list of packages. You can then search for and install Flask, Flask-Script, and other packages from the Python Package Index.

Using the Command Line

Note

Some of the department computers do not have it, in which case you can use the older virtualenv tool. Just replace pyvenv or pyvenv-3.4 with virtualenv -p /usr/bin/python3.4.

To create a virtual environment, run the following command in the terminal on Unix:

$ pyvenv /path/to/my/venv

You might need to use pyvenv-3.4 or pyvenv-3.5 instead of just pyvenv. Replace /path/to/my/venv with the path to where you want your virtual enviroment installed, such as ~/cs3320-env or C:\Users\myuser\cs3320-env.

On Windows, pyvenv is not in the path by default. It is located in C:\Python34\Tools\Scripts. We can run it with its full path as follows:

C:\Users\michael> python C:\Python34\Tools\Scripts\pyvenv.py cs3320-env

Since we are running this command in C:\Users\michael, it will create the virtual environment at C:\Users\michael\cs3320-env.

Note

Python 3.5 installs to a different location on Windows, under your user profile's AppData directory.

This will create a virtual environment at the specified location. It will also install pip, a Python package manager, into the environment, and arrange for the python executable to be the specified version of Python.

Using the Virtual Environment in PyCharm

You can add the virtual environment just like any other Python installation in PyCharm when you configure your Project Interpreter (when you create the project, or after the fact in File -> Settings).

If your virtual environment doesn't appear in the list, click the browse button and browse to the bin/python executable (on Unix) or python.exe (on Windows) file that it contains. PyCharm will automatically scan it for packages and make it available.

Using the Virtual Environment at the Command Prompt

If you want to use Python from the command prompt, you first need to activate the virtual environment. This needs to be done each time you open a terminal to use the environment (activation adds the virtual environment to the current terminal session).

On OS X and Linux, assuming you are using the default bash shell, you can run the following to activate:

source /path/to/venv/bin/activate

If you are using PowerShell on Windows, run:

C:\Path\to\venv\Scripts\activate.ps1

Note

If PowerShell says that the policy does not allow the script to be run, change the execution policy to RemoteSigned by running the following:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

In the legacy command prompt, you can use the activate.bat file and it will function just like activate.ps1.

Once you have activated the virtual environment, your python and pip commands will run the isolated Python.

Installing Packages

Python libraries, such as Flask, can be installed with the pip tool. All the libraries we use are Pip-installable.

To install the basic libraries we need for all of our projects, run the following in a terminal where you have activated your virtual environment:

pip install Flask Flask-Script

Flask is the main Flask toolkit, and Flask-Script is the library that makes our manage.py files work (see Assignment 0).

You can also install packages from within PyCharm. Go to the ‘Project Interpeter’ settings, and click the ‘+’ button at the bottom of the package list.