GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing www.pythonmorsels.com — Using virtual environments in Python Watch this as a 03:57 screencast
Same content, narrated.
Show captions
Autoplay
Sign in to change your settingsSign in to your Python Morsels account to save your screencast settings. Don't have an account yet? Sign up here. Let's talk about using virtual environments in Python. Python code with third-party requirementsHere we have a Python program called Currently our The Before we go about The basics of virtual environmentsVirtual environments are a way to isolate the dependencies needed for each Python project on our machine. Let's start using a virtual environment for our project. We're going to:
Creating a new virtual environment with
|
| Environment | Command |
|---|---|
| Windows Command Prompt (CMD) | .venv\Scripts\activate.bat |
| Windows Power Shell | .venv\Scripts\Activate.ps1 |
| Linux/Mac bash, zsh, & dash | source .venv/bin/activate |
| Linux/Mac C Shell | source .venv/bin/activate.csh |
| Linux/Mac fish | source .venv/bin/activate.fish |
| Linux/Mac Xonsh | source .venv/bin/activate.xsh |
On my machine, I'll need to run that third command (source .venv/bin/activate) to activate this new virtual environment:
$ source .venv/bin/activate
Now, I can tell that I'm in an activated virtual environment because my prompt has changed.
(exponential) $
See those parentheses at the beginning of our prompt with the name of our virtual environment (exponential) within it?
When we created our virtual environment we used the --prompt command to set its name to the name of our project (exponential), but by default the virtual environment prompt would be the name of our virtual environment directory (.venv in our case).
If we use pip to list the current packages we have installed, we'll see that we don't see all those globally-installed packages we saw earlier:
(exponential) $ python3 -m pip list
Package Version
---------- -------
pip 22.0.4
We don't see our global packages because we're in our virtual environment which is isolated from the packages that we've installed globally on our machine.
Now that we've created and activated our virtual environment, we need to install a Python package, using pip!
If we use pip to install rich at this point, we'll install it specifically into our activated virtual environment, not globally on our machine:
(exponential) $ python3 -m pip install rich
Collecting rich
Using cached rich-12.4.1-py3-none-any.whl (231 kB)
...
Installing collected packages: commonmark, pygments, rich
Successfully installed commonmark-0.9.1 pygments-2.12.0 rich-12.4.1
If we run pip list, you can see that we just installed rich as well as some of its dependencies:
(exponential) $ python3 -m pip list
Package Version
---------- -------
commonmark 0.9.1
pip 22.0.4
Pygments 2.12.0
rich 12.4.1
And if we run our exponential.py program at this point, we'll see that it works now!
(exponential) $ python3 exponential.py 1000 7
┏━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ Year ┃ Old ┃ Added ┃ New ┃
┡━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ 1 │ 1,000 │ 70 │ 1,070 │
│ 2 │ 1,070 │ 75 │ 1,145 │
│ 3 │ 1,145 │ 80 │ 1,225 │
│ 4 │ 1,225 │ 86 │ 1,311 │
│ 5 │ 1,311 │ 92 │ 1,403 │
│ 6 │ 1,403 │ 98 │ 1,501 │
│ 7 │ 1,501 │ 105 │ 1,606 │
│ 8 │ 1,606 │ 112 │ 1,718 │
│ 9 │ 1,718 │ 120 │ 1,838 │
│ 10 │ 1,838 │ 129 │ 1,967 │
│ 11 │ 1,967 │ 138 │ 2,105 │
│ 12 │ 2,105 │ 147 │ 2,252 │
│ 13 │ 2,252 │ 158 │ 2,410 │
│ 14 │ 2,410 │ 169 │ 2,579 │
│ 15 │ 2,579 │ 180 │ 2,759 │
└──────┴───────┴───────┴───────┘
Our program works because it found the rich module that it needed to print out that table representing exponential growth.
Note that if we open a new terminal window and we run our program in that new terminal window, we'll see that it doesn't work:
$ python3 exponential.py 1000 7
Traceback (most recent call last):
File "/home/trey/exponential/exponential.py", line 2, in <module>
from rich.console import Console
ModuleNotFoundError: No module named 'rich'
Our program doesn't work in this new terminal window because virtual environments are activated for just the window you have open; they're not activated for your whole machine.
In order to use our virtual environment in a new window, we would need to run the activation command again:
$ source .venv/bin/activate
After activating our virtual environment, our code will work again:
(exponential) $ python3 exponential.py 1000 7
┏━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ Year ┃ Old ┃ Added ┃ New ┃
┡━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ 1 │ 1,000 │ 70 │ 1,070 │
│ 2 │ 1,070 │ 75 │ 1,145 │
│ 3 │ 1,145 │ 80 │ 1,225 │
│ 4 │ 1,225 │ 86 │ 1,311 │
│ 5 │ 1,311 │ 92 │ 1,403 │
│ 6 │ 1,403 │ 98 │ 1,501 │
│ 7 │ 1,501 │ 105 │ 1,606 │
│ 8 │ 1,606 │ 112 │ 1,718 │
│ 9 │ 1,718 │ 120 │ 1,838 │
│ 10 │ 1,838 │ 129 │ 1,967 │
│ 11 │ 1,967 │ 138 │ 2,105 │
│ 12 │ 2,105 │ 147 │ 2,252 │
│ 13 │ 2,252 │ 158 │ 2,410 │
│ 14 │ 2,410 │ 169 │ 2,579 │
│ 15 │ 2,579 │ 180 │ 2,759 │
└──────┴───────┴───────┴───────┘
What do we do when we want to leave our virtual environment? To exit a virtual environment, you can deactivate it.
To deactivate a virtual environment, we could just exit the window we have open or open up a new window. As long as we open up a fresh terminal window, we'll be in a window that doesn't have a virtual environment open.
To deactivate our virtual environment and keep using the same window, we can run the deactivate command from within our activated virtual environment:
(exponential) $ deactivate
Now we're back to our usual prompt (without that (exponential ) prefix):
~/exponential $
Virtual environments are a way to isolate the dependencies that you use for each Python project that you work on.
It's considered a best practice to use a separate virtual environment for each Python project on your machine.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
Python's pip package isn't just for installing packages. You can also use pip to keep track of which packages (and which package versions) your code depends on.