Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Virtual Environments and pyenv

Give Feedback

Here are resources about working with virtual environments:

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.

00:18 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.

00:35 If you’re wondering what the difference is between pyenv, pyenv-virtualenv, and tools like virtualenv or venv, then don’t worry. You’re not alone.

00:48 Here’s what you need to know. pyenv manages multiple versions of Python itself. virtualenv or venv manages virtual environments for a specific Python version.

01:04 pyenv-virtualenv manages virtual environments across varying versions of Python.

01:12 If you’re a die-hard virtualenv or 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.

01:37 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.

01:52 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.

02:12 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.

02:45 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 versions/ directory.

03:07 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.

03:25 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.

03:46 This sets the value of the .python-version file in your current working directory to the virtual environment name.

03:57 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.

04:25 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.