Here are resources about working with virtual environments:
Virtual Environments and pyenv
00:00 Virtual environments are a big part of managing Python installations and applications. If you haven’t heard of virtual environments before, you can check out the video course Working With Python Virtual Environments here on Real Python.
Virtual environments and
pyenv are a real match made in heaven.
pyenv has a wonderful plugin called
pyenv-virtualenv that makes working with multiple Python versions and multiple virtual environments a breeze.
If you’re wondering what the difference is between
pyenv-virtualenv, and tools like
venv, then don’t worry. You’re not alone.
Here’s what you need to know.
pyenv manages multiple versions of Python itself.
venv manages virtual environments for a specific Python version.
pyenv-virtualenv manages virtual environments across varying versions of Python.
If you’re a die-hard
venv user, don’t worry:
pyenv plays nicely with either. In fact, you can keep the same workflow you’ve had if that is what you prefer, though I think
pyenv-virtualenv makes for a nicer experience when you’re switching between multiple environments that require different Python versions.
The good news is that since you used the
pyenv-installer script to install
pyenv, you already have
pyenv-virtualenv installed and ready to go.
So, let’s create your first virtual environment using
pyenv. Open the terminal, make sure you are in the project directory, and type
pyenv virtualenv 3.9.1 pyenv-course.
With this command, you created a new virtual environment named
pyenv-course, which uses Python version 3.9.1. Technically, specifying the Python version is optional, but you should consider always specifying it so you’re certain of what Python version you’re using. The environment name is just a name for you to keep your environments separate, and a good practice is to name your environments the same as your project.
Let’s have a look in the
.pyenv/versions/ directory, where you will see that the
pyenv virtualenv command created a virtual env inside the 3.9.1 version and added a symlink to that virtual env in the
When you have a look at the current output of
pyenv versions, you will see that the newly-created virtual env is also added, which means you can treat a virtual env the same as any other installed Python version.
Now let’s use the virtual environment you’ve just created by running
pyenv local pyenv-course inside the project directory. You’ve seen the
pyenv local command before, but this time instead of specifying a Python version, you specify an environment.
This sets the value of the
.python-version file in your current working directory to the virtual environment name.
You can see the
python executable is pointing to the virtual environment when you run
pyenv which python. If you look at any executable this environment provides, you will see the same thing. Take, for example,
pyenv which pip, which will show you that the
pip executable is also located in the virtual environment.
Now that you know how to create virtual environments with
pyenv, let’s put everything together in the next lesson and work with multiple environments.
Become a Member to join the conversation.