DEV Community

Cover image for Pyenv and Virtualenvs Quick-start
Rodrigo Medina
Rodrigo Medina

Posted on

Pyenv and Virtualenvs Quick-start

Image obtained from DALL-E: epic virtual pythons yellow and blue with a cyberpunk style

Whenever I get a new computer, I need to get through the python versions management, so this is the guide with the most common commands I need to execute to get up and running.

For this, I will use pyenv and the pyenv-virtualenv tools.

Initial help

This should be the most common first step for most of the tools we use.

# Get the available commands for pyenv
pyenv --help
Enter fullscreen mode Exit fullscreen mode

Get The Installed Python Versions

This command will show us which versions we have already installed in our system. In the beginning, it should be only system, which is the default installation that comes with the macOS.

## List all the python versions available to pyenv
pyenv versions
Enter fullscreen mode Exit fullscreen mode

Install A New Version

First, we should run the --help command, to get the available subcommands available in the install option.

# Generic help & usage
pyenv install --help
Enter fullscreen mode Exit fullscreen mode

Then, we need to decide which version we want to install. For that, we may want to first see all the available versions. To achieve this, we need to execute:

# List all available versions
pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Finally, we need to execute the installation command with the version we want. As an example, I will install the 3.9.14 version.

# Install selected version
pyenv install 3.9.14
Enter fullscreen mode Exit fullscreen mode

To verify our installation, we may execute pyenv versions again, and the recent installation should appear there.

Use The Recently Installed Python Version

To use the recently installed as the global, or default version, we need to execute:

# Sets the global Python version
pyenv global 3.9.14
Enter fullscreen mode Exit fullscreen mode

Create a Virtual Environment

The best practice for python projects, is to use a virtualenv per project, so we can have isolated dependencies between them. To create a new virtualenv we need to execute:

# Create a new virtual env with the global python version
# pyenv virtualenv YOUR_VIRTUAL_ENV_NAME
# e.g.
pyenv virtualenv python-graphql-client
Enter fullscreen mode Exit fullscreen mode

List All the Virtual Environments

A good way to verify that a virtual env has been created successfully is to list all the available virtual environments. To achieve this, we execute:

# Show available virtualenvs
pyenv virtualenvs
Enter fullscreen mode Exit fullscreen mode

Activate The Virtual Environment

To activate, and start using your virtual environment, you need to execute:

# Activate a virtualenv
# pyenv activate YOUR_VIRTUAL_ENV_NAME
# e.g.
pyenv activate python-graphql-client
Enter fullscreen mode Exit fullscreen mode

Deactivate The Virtual Environment

Once you're done using your virtual environment, you may want to deactivate it. To achieve this, you may run:

# The deactivation must be sourced
source deactivate
Enter fullscreen mode Exit fullscreen mode

Extra

If you want to avoid activating and deactivating manually, you can use aactivator, which will activate and deactivate automatically based on the project you are placed.

Conclusions

This is the basic stuff that works for me. If you find it useful, consider giving a like. Happy hacking!

Top comments (0)