Minimal usage of pyenv and venv

Tram Ho

Introduction

I’m developing with Python + Django, but if I’m in charge of multiple projects, the versions of Python, Django, and other libraries will differ depending on the project.

In some cases, the same Python version has different versions of different libraries, so versioning with pyenv and venv is very helpful. This time, I will focus on the minimum necessary functions and describe how to use it.

Deploy Environment

OS:macOS Big Sur(version 11.2.2)
Shell :zsh

or

OS:CentOS7
Shell:bash

pyenv

By using pyenv, it is possible to use various versions of Python.

At the time of this article, you can install from 2.1.3 to 3.9.1.

Install pyenv

In case of mac, it seems that it can be installed using Homebrew.

Linux users can make it with git.

Edit shell config file

Check your login shell with echo $ SHELL.
zsh → .zshrc (.zsh_profile)
bash → .bash_profile

The following is described in. You can write it directly in vim, but this time we will use echo for simplification.

Finally, the setting file is reflected.

Install the required version of Python with pyenv

Since pyenv is available in the previous settings, check the available Python version.

Those displayed in the list as ○. ○. ○ can be installed.

Install the version of pyenv you want to use.

pyenv version switching

Check the current version.

The system and installed version are displayed.

The version listed next to the “*” is the one used in the current directory.

Switch the version of Python to use.

It can be subdivided according to the directory, but it is omitted this time because the main purpose is to use venv.

If you check again with pyenv versions, the default version has been switched. (You can also check by starting Python with the interpreter)

Now you are ready to create a venv virtual environment with the version you want to use.

Create venv environment

Since venv is available by default in Python 3 series, there is no need to install it (if you are using 2 series, you can perform similar version control by installing virtualenv).

Execute the following in the directory where you want to create the virtual environment.

Since the venv_project directory is created under it, move it with cd venv_project and activate the virtual environment.

If the execution is successful, it will be displayed before the host name. You are now in the virtual environment.

By activating the virtual environment, the libraries to be installed with pip can be managed independently from the local environment.

After that, feel free to install and use the required version of the library.

The last time you return to your environment locally, deactivate it.

With the above, it seems that the minimum version control with python can be done.

Finally

If you understand both pyenv and venv properly, you can use it more conveniently.

However, if you follow this procedure, the local environment will remain in its original state, so I think you can prevent conflicts in the version.

I hope it will be helpful to anyone.

Share the news now

Source : Viblo