DEV Community

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

Posted on

7

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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay