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 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.
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.
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.
Become a Member to join the conversation.