|
1 | | -# Virtual Environments |
| 1 | +# Virtual Environments Primer |
| 2 | + |
| 3 | +This document is meant as a supplement to our Installing Python series: |
| 4 | + |
| 5 | +- [Installing Python for macOS](https://github.com/stanfordpython/python-handouts/blob/master/installing-python-macos.md) |
| 6 | +- [Installing Python for Linux](https://github.com/stanfordpython/python-handouts/blob/master/installing-python-linux.md) |
| 7 | +- [Installing Python for Windows](https://github.com/stanfordpython/python-handouts/blob/master/installing-python-windows.md) |
| 8 | + |
| 9 | +## Using virtual environments |
| 10 | + |
| 11 | +Once you have a virtual environment set up for a project or for a course, there's only one important thing to remember. **Activate your virtual environment every time you open a new shell.** We'll see what this means and how to do this later in this document. For now, if you only take away one thing, it should be that you should run `source ~/cs41-env/bin/activate` every time you open a new shell. If you want to get fancy, you can even add this line to your shell startup script (like `~/.bashrc` or `~/.bash_profile`) so it is executed automatically on shell startup. |
2 | 12 |
|
3 | 13 | ## What is a virtual environment? |
4 | | -A virtual environment is simply an isolated folder that contains all the executables you need to build something awesome in Python, including code to run the interpreter, use the standard library (https://docs.python.org/3.4/library/), and install other 3rd-party libraries that we'll touch on towards the end of the quarter. |
| 14 | +A virtual environment is simply an isolated folder that contains all the executables you need to build something awesome in Python, including code to run the Python interpreter, use [the standard library] (https://docs.python.org/3/library/), and install other third-party libraries that we'll need later in the quarter. Importantly, a virtual environment will keep all of these things in one place (inside the virtual environment folder), so that your local development environment is isolated from the rest of your computer. |
5 | 15 |
|
6 | 16 | ## Why bother using virtual environments? |
7 | | -Of course, you can also develop Python projects without a virtual environment, but it's a good idea to become familiar with this tool because you can avoid dependency conflicts between your system-wide Python installations and your project-specific Python installations. For example, if you're using Python to build 3 separate Web apps because you're a beast, Project A might depend on Django version 1.2 while Project C depends on Django version 1.9 (yes, Django is the name of a real Web framework: https://docs.djangoproject.com/en/1.9/releases/). You can create a separate virtual environment for each project then install whichever version you need in each project. When you ship awesome projects later in your Python career, you'll know the exact libraries and versions that your project requires (: |
| 17 | +Of course, you could also develop Python projects without a virtual environment, but it's a good idea to become familiar with this tool. |
| 18 | + |
| 19 | +Suppose that you want to use version 2 of the `foo` library but another application requires version 1 of `foo` (this comes up a lot!). How can you have both these applications on your computer at the same time? If you install everything into `/usr/lib/python3.7/site-packages` (or whatever your platform's standard location is), you could easily end up in a sticky situation, in which you might unintentionally upgrade an application that shouldn't be upgraded. |
| 20 | + |
| 21 | +More generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application. |
| 22 | + |
| 23 | +Also, perhaps you can't install packages into the global directory, maybe because you're on a shared host like `rice`. |
| 24 | + |
| 25 | +In all of these cases, a virtual environment can isolate a project's development and execution environment and make programming in Python a breeze. |
| 26 | + |
8 | 27 |
|
9 | | -## How to create virtual environment |
10 | | -In class we audibled to using a piece of software called virtualenv to build virtual environments because some people had trouble with the virtualenvwrapper software. After you've installed Python3 and pip (see https://github.com/stanfordpython/python-handouts/blob/master/installing-python.md#linux if you need help with this step), you're almost done. Here's the rest of the commands to execute: |
| 28 | +## Creating a virtual environment |
| 29 | +In Python 3.6+, the recommended way to create a virtual environment is to run: |
11 | 30 |
|
12 | | -### Install the software (only do this once) |
13 | | -`$ pip install virtualenv` |
| 31 | +``` |
| 32 | +$ python3 -m venv /path/to/new/virtual/environment |
| 33 | +``` |
14 | 34 |
|
15 | | -this uses pip to install the software used to build your virtual environments, called virtualenv |
| 35 | +Make sure that `python3` resolves to whichever version of Python 3 you'd like to bind to your virtual environment. |
16 | 36 |
|
17 | | -### Creating a virtual environment |
18 | | -cd to your Desktop then execute |
| 37 | +For example, to create a new virtual environment for CS41 named `cs41-env` in your home folder, you could run: |
19 | 38 |
|
20 | | -`$ virtualenv cs41` |
| 39 | +``` |
| 40 | +$ python3 -m venv ~/cs41-env |
| 41 | +``` |
21 | 42 |
|
22 | | -this uses virtualenv to create a new virtual environment in the working directory named cs41. you should be able to see this folder on your Desktop now. |
| 43 | +## Activation and Deactivation |
| 44 | +To activate a virtual environment on macOS or Linux running `bash` or `zsh`, `source` the following path: |
23 | 45 |
|
24 | | -### Activating and deactivating a virtual environment |
25 | | -To activate, simply cd into the environment we created above then execute |
| 46 | +``` |
| 47 | +$ source ~/cs41-env/bin/activate |
| 48 | +``` |
26 | 49 |
|
27 | | -`$ source bin/activate` |
| 50 | +You can replace `~/cs41-env/bin/activate` with the corresponding path if you created your virtual environment in a different location. |
28 | 51 |
|
29 | | -Now you can play with the interactive interpreter inside your isolated environment using |
| 52 | +If you're using a different shell, you'll need to run a different command: |
30 | 53 |
|
31 | | -`$ python3` |
| 54 | +- `fish`: `$ . ~/cs41-env/bin/activate.fish` |
| 55 | +- `csh/tcsh`: `$ source ~/cs41-env/bin/activate.csh` |
| 56 | +- `cmd.exe` (Windows): `> ~\cs41-env\Scripts\activate.bat` |
| 57 | +- `PowerShell` (Windows): `PS> ~\cs41-env\Scripts\Activate.ps1` |
| 58 | + |
| 59 | +When a virtual environment is activated, you will see a prefix on your prompt, so it will look like this: |
| 60 | + |
| 61 | +``` |
| 62 | +(cs41-env)$ |
| 63 | +``` |
| 64 | + |
| 65 | +Now that we have an active virtual environment, we can play around with it: |
| 66 | + |
| 67 | +``` |
| 68 | +(cs41-env)$ python --version |
| 69 | +(cs41-env)$ pip --version |
| 70 | +(cs41-env)$ python |
| 71 | +>>> print("Hello world!") |
| 72 | +Hello world! |
| 73 | +>>> quit() |
| 74 | +(cs41-env)$ |
| 75 | +``` |
| 76 | + |
| 77 | +You can create new files, modules, and whatever else you need to make a beautiful Python project! |
| 78 | + |
| 79 | +Most importantly, when a virtual environment is activated, you can install Python packages into this virtual environment using `pip`. For example, to install the numerical Python package `numpy`, run: |
| 80 | + |
| 81 | +``` |
| 82 | +(cs41-env)$ pip install numpy |
| 83 | +``` |
32 | 84 |
|
33 | | -You can also create new files, modules, whatever you need to make a beautiful Python project ready to ship! |
34 | 85 | To deactivate, type |
35 | 86 |
|
36 | | -`$ deactivate` |
| 87 | +``` |
| 88 | +(cs41-env)$ deactivate |
| 89 | +$ |
| 90 | +``` |
| 91 | + |
| 92 | +That's it! |
| 93 | + |
| 94 | +## Last words and alternatives |
37 | 95 |
|
38 | | -All done! If you want to read more about the above steps, check out http://docs.python-guide.org/en/latest/dev/virtualenvs/ |
| 96 | +To reiterate, **activate your virtual environment every time you open a new shell.** |
39 | 97 |
|
40 | | -If you want to read about the interactive interpreter, check out http://www.python-course.eu/python3_interactive.php |
| 98 | +Also, know that you should not move your virtual environment folder (in the above, that's `~/cs41-env`), as it is subject to break if it is relocated. |
41 | 99 |
|
| 100 | +There are also some alternatives to virtual environments that operate at a higher level, but are a little more complicated to set up. You can use `virtualenvwrapper` to manage virtual environments in a single place, or the even-higher-level tool `pipenv` to automatically handle everything. You can read more [here](https://docs.python-guide.org/dev/virtualenvs/). |
42 | 101 |
|
| 102 | +## Credit |
43 | 103 |
|
| 104 | +Much credit goes to [Python's `venv` documentation](https://docs.python.org/3/library/venv.html) and numerous other online tutorials. |
44 | 105 |
|
45 | 106 |
|
0 commit comments