Contents
Outline
When you develop many Python projects on the same machine(PC), you need to make each environment by the Python version of each project.
In this case, you can’t remove and install the Python version every time for developing each project. In this blog post, I will introduce how to use pyenv
to install and manage various Python versions on the single machine..
Install pyenv
Execute the following command to install pyenv
.
- macOS:
brew install pyenv
- Windwos:
choco install pyenv-win
For macOS
, open the .zshrc
file and modify it like the following.
# code ~/.zshrc
...
# pyenv setting
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Install pyenv-virtualenv
pyenv-virtualenv
is a plugin of pyenv
to help us make the Python virtual environment.
- pyenv-virtualenv: https://github.com/pyenv/pyenv-virtualenv
Execute the following command to install pyenv-virtualenv
.
- macOS:
brew install pyenv-virtualenv
- Windows:
pip install pyenv-virtualenv
For macOS
, open the .zshrc
file and modify it like the following.
# code ~/.zshrc
...
# pyenv-virtualenv setting
eval "$(pyenv virtualenv-init -)"
Python version lst
Execute the following command to check installable Python versions by pyenv
.
pyenv install --list
And then, you can see installable Python version list like the following.
...
stackless-3.2.5
stackless-3.3.5
stackless-3.3.7
stackless-3.4-dev
stackless-3.4.2
stackless-3.4.7
stackless-3.5.4
stackless-3.7.5
Install Python 3.9.9
In this blog post, I install the Python 3.9.9
version. Execute the following command to install the Python 3.9.9
version.
pyenv install 3.9.9
pyenv versions
After installing, execute the following command to check installed Python version.
pyenv versions
And then, you can see the Python 3.9.9
version just installed.
* system (set by /.pyenv/version)
3.9.9
pyenv version
You can check current Python version by executing the following command.
pyenv version
When you execute the command, you can see current Python version like the following.
system (set by /Users/dev-yakuza/.pyenv/version)
Use installed Python version
Next, let’s see how to use the Python version installed by pyenv
. Execute the following command to use Python 3.9.9
globally.
pyenv global 3.9.9
Execute the following command to use it on the project where you execute the command same directory.
pyenv local 3.9.9
This command will create the .python-version
file and when pyenv
is activated, the corresponding version of Python is set automatically.
Execute the following command to use the Python version in the current shell
.
pyenv shell 3.9.9
Create Python virtual environment
Multiple projects may use the same Python version, but may have different versions of the library they install. In this case, the unintended version of the library may be used and cause problems.
For this, you can solve this problem by creating a virtual environment for each project using the same version of Python and developing on the virtual environment.
So, let’s create the virtual environment by pyenv-virtualenv
. Execute the following command to create the Python virtual environment.
# pyenv virtualenv VERSION NAME
pyenv virtualenv 3.9.9 venv
After creating, execute the following command to check the Python virtual environment created well.
pyenv versions
And then, you can see the virtual environment created well like the following.
* system (set by /.pyenv/version)
3.9.9
3.9.9/envs/venv
venv
Next, execute the following command to activate the virtual environment created by pyenv-virutalenv
.
pyenv activate venv
And then, execute the following command again to check the virtual environment activated well.
pyenv versions
If there is no problem, you can see the virtual environment activate well.
system
3.9.9
3.9.9/envs/venv
* venv (set by PYENV_VERSION environment variable)
When you’re done with the virutal environment, you can deactivate the virtual environment by executing the following command.
pyenv deactivate
Execute the following command again to check the virtual environment deactivated well.
pyenv versions
And then, you can see the virutal environment deactivated well like the following.
* system (set by /.pyenv/version)
3.9.9
3.9.9/envs/venv
venv
.python-version file
Earlier, we’ve seen the .python-version
file is created automatically when we exeute the pyenv local
command. pyenv
will use it to change the Python
version automatically for the development environment. If your environment doesn’t have the Python version in the file, you can execute the following command to install the Python version of the .python-version
file.
pyenv install
So, you can share the Python version for the corresponding project by using the .python-version
file. If the current project doesn’t have the .python-version
file, you can create the .python-version
file and write required Python version of the project on it like the following.
3.9.9
Delete Python version
If you have a Python version or Python virtual environment on your machine that you no longer use, you can delete the version(virtual environment) with the following command.
pyenv uninstall 3.9.9
# pyenv uninstall venv
Completed
Done! we’ve seen how to use pyenv
to install and manage various Python versions on the same machine. Also, we’ve seen how to use the pyenv-virtualenv
plugin to make the Python virtual environment. If you need many Python version of the development environment, try to use pyenv
to manage the versions.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.