How to install pyenv+pipenv in ubuntu and use multiple versions of python and its suites
Let pyenv and pipenv help you write python better
Co-Author: Y. S. Huang, a master’s student studying AIVC, likes open-source.
If you are interested, go to check my Github!
Content
pyenv
Why do we need multiple versions of python?
pipenv
Why do we need multiple versions of python?
As an engineer, you may face many projects at the same time, but each project may require different versions of python. At this time, as a qualified engineer, it is impossible to uninstall python every time you need to switch versions and reinstall. In this situation, you will need a tool that can install and switch different versions of python, this is the protagonist pyenv
introduced today.
How to install Pyenv?
1. First we need to install git (if it is already installed, skip it)
sudo apt-get install git
2. Use apt-get to install related packages
sudo apt-get update; sudo apt-get install -y --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
3. Download pyenv
from the official GitHub with git clone
to install:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
4. This is the most important and error-prone step, allowing our terminal to use the instructions of pyenv
. In this step, we need to make different settings according to the shell used by your terminal. The following is an example of the default bash
of Ubuntu and the zsh
used by the moderator:
bash users: set .bashrc and .profile
The simplest and most violent method is to directly copy and execute the following scripts:
echo -e ‘if shopt -q login_shell; then’ \
‘\n export PYENV_ROOT=”$HOME/.pyenv”’ \
‘\n export PATH=”$PYENV_ROOT/bin:$PATH”’ \
‘\n eval “$(pyenv init — path)”’ \
‘\nfi’ >> ~/.bashrcecho -e ‘if [ -z “$BASH_VERSION” ]; then’\
‘\n export PYENV_ROOT=”$HOME/.pyenv”’\
‘\n export PATH=”$PYENV_ROOT/bin:$PATH”’\
‘\n eval “$(pyenv init — path)”’\
‘\nfi’ >>~/.profileecho ‘if command -v pyenv >/dev/null; then eval “$(pyenv init -)”; fi’ >> ~/.bashrc
Finish!
zsh user: set ~/.zprofile
and ~/.zshrc
of zsh
Simpler for zsh users:
echo ‘export PYENV_ROOT=”$HOME/.pyenv”’ >> ~/.zprofile
echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”’ >> ~/.zprofile
echo ‘eval “$(pyenv init — path) “‘ >> ~/.zprofile
echo ‘eval “$(pyenv init -)”’ >> ~/.zshrc
Finish!
The first three lines of instructions mean adding the following text to the ~/.zprofile
file
export PYENV_ROOT=”$HOME/.pyenv
export PATH=”$PYENV_ROOT/bin:$PATH
eval “$(pyenv init — path)”
The last line of instruction means to add the following text to the ~/.zshrc
file
eval “$(pyenv init -)”
Of course, you can also open the file directly and paste these lines. However, if you are watching other teachings at the same time, be careful to delete the newly added text in other teachings first, and then paste these lines!
5. Finally, log in again, let the computer re-read the .zproflie file and you can use it. You can also use source ~/.profile
or source ~/.zprofile
for the computer to reload the config file again.
Possible errors:
zsh: command not found: pyenv
This means that a few lines of instructions in .zprofile have not been executed. You can use the
source
command to make the computer read. If it is still the same after re-login, you can put the text originally added to .zproflie into.zshrc
, and you can put the newly addedeval “$(pyenv init -)”
is deleted, and finally the terminal can be re-executed.If you want to know the execution order of
~/.profile
and~/.zshrc
, you can refer to the follow link down below, probably when you log in,~/.zproflie
, then~/.zshrc
will be executed when terminal is opened.If jump out
ValueError: unknown locale: UTF-8
related errors.This is due to a LANG error in
.zshrc
,try changing it to the following
export LANG=en_US.utf-8
How to use pyenv
?
1. Check which Python versions are available
To see which Python versions are available for installation, use the command pyenv install --list
or pyenv install -l
.
2. Install Python
Take creating a virtual environment for Python 3.8.12 as an example:
pyenv install 3.8.12
The message after execution is:
Downloading Python-3.8.12.tar.xz…
-> https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tar.xz
Installing Python-3.8.12…
Just wait for it to install.
3. Set 3.8.12 as the Python version in use
pyenv global 3.8.12
3.8.12 can be replaced with any version you have installed
The so-called “global” here is only valid for the current user’s shell. In addition, the board owner does not recommend using the
pyenv local <python-version>
command here, because using local may lead to the wrong location when using the virtual environment to install the package later. Of course, if you very clearly know what kind of environment you want a locate, then enjoy using it!
4. Check the version of python and pyenv
View the currently available python version that has been installed (there will be a *
in front of it):
pyenv versions
Check out the current python version in use:
pyenv version
Check the current version of pyenv
in use:
pyenv --version
Why do you need pipenv
?
As a qualified engineer, you will find that in addition to Python itself, there are many versions, and even the packages under python will have many versions, but pyenv
can’t handle so many. What should I do at this time? And in the process of collaborating with other engineers, you will also need to co-work on a project. Is there has an easy way to transfer it along with the packages it needs?
At this time, you need pipenv
to save yourself from repeated uninstallation and installation.
What is pipenv
?
It integrates pip3
and virtualenv
into one. It has both functions and better optimization so that you can create a virtual environment under python with a single command by using pipenv
to install python’s specified packages and many more. Things and the various virtual environments will not affect each other. The most important thing is that a version directory file is automatically generated. When handing over the work, as long as it is delivered together with the program file, the other party will know what version of python and packages should be used, and let Pipenv install it automatically.
How to install pipenv
?
Here is the pip3
with the latest python3
sudo apt-get install python3-pip
After installation, you can use the command to check the version of the pip/pip3
pip --version
Use this command to see what packages you have installed
pip list
Usually, there are a lot of things installed in the system by default.
Next use pip
to install pipenv
pip install pipenv
Finish!
Compared with pyenv
it is much simpler. Finally, use the following command to confirm the version you installed.
pipenv --version
How to use pipenv
?
Create a virtual environment
In the desired path, using pipenv
to create a virtual environment of the python version you want to use, take python 3.8.6 as an example:
pipenv --python 3.8.6
At this time, if there is no Pipfile file in the directory, pipenv
will automatically generate a Pipfile to record the installed packages, the content is as follows:
Enter the virtual environment
Execute the following command to enter the virtual environment
pipenv shell
If you are also using oh-my-zsh with powerlevel10k, the name of the virtual environment should appear in the upper right corner of the command, indicating that you have entered the virtual environment
If you want, you can also install plugins of pipenv on oh-my-zsh so that you can manipulate the pipenv
easier & faster.
Install the desired package
Install the desired package in the virtual environment, taking NumPy as an example
pipenv install numpy
Install a specific version of the package:
pipenv install numpy==1.16.5
Uninstall unwanted packages:
pipenv uninstall numpy
After the installation is complete, the ./pipfil
e will also change:
If you accidentally install an error when using
pipenv
install, such as typing the wrong package name or the package name does not exist, etc,.
At this point you usually change the name and download it again, right? But you will find that even though the name of the kit is correct this time, the same error message will still be sprayed! ! !
This is because even if the last package name cannot be downloaded due to the wrong name,pipenv
still adds the wrong package name to the Pipfile file, and because the package that is not installed in Pipflie will be installed before downloading the new package this time. The new package will be installed, at which pointpipenv
will be forced to download the package with the wrong name again…
The solution is: directly rewrite the Pipfile file and remove the wrong package name! ! ! Or use pipenv uninstall with the wrong package name. Uninstalling the package will also automatically remove the wrong package name in the Pipfile! ! !If the error is that the pip related instructions cannot be found in the virtual environment, you can use
python get-pip.py — force-reinstall
to reinstall pip, so that it can be used.
Taken from:
In addition, after the package is installed, a pipfile.lock file will be generated, which is the file that stores all the required package settings. Just pass this file to the cooperating engineer, and he can use the pipenv
sync command to All required kits can be installed.
Exit the virtual environment
Press CTRL + D
or hit exit
to exit the virtual environment.
Delete virtual environment
To delete the virtual environment, execute pipenv --rm
command.
Why use pyenv
+pipenv
?
pyenv
allows each version of python on our computer to be managed separately, and pipenv
can create a new independent package-space under each python version, allowing you to install the required packages in it so that each things in the space are independent of each other and do not affect each other. It’s easier to manage and much more organized. When you want to change the suite, the scope of the impact will only be limited to that small space, and will not cause too much complexity when you make changes in the future due to the lack of organized management because of too many things installed. You need to reload the computer directly.