Overview
This story is about how to easily manage multiple versions of python on the system level. Secondly, how to install package versions globally and locally(on virtual environments)
There is a solution already available named pyenv to manage the versions. But I want to access the python version on system-level and don’t want to switch between different environments.
Assumptions
I am assuming that a python version already exists on your system and using yum as your package manager.
Pre-Requisites
- Virtual environment
Guidelines
- Install all the dependencies that are required by python3.
$ sudo yum -y install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel
2. Use this link to download the source.
3. Untar and build it
$ tar xJf <file-name>.tar.xz
$ cd <file-name>.tar.xz
$ ./configure
$ make
$ sudo make altinstall # if a version is already installed other remove the "alt" and just run install
4. Check it is available or not.
$ python3 -version
5. If it is not available check this directory /usr/local/bin. If it exists in the /usr/local/bin then move it to /usr/bin. Because I want it to be accessible across all system users. Move these files
pythonX.X
pythonX.Xm
pythonX.Xm-config
6. It should be accessible now.
7. To install a package for a specific version of python use this command:
sudo python<version> -m pip install <package-name>
It stores installed packages on this path
/usr/local/lib/pythonX.X/site-packages
8. There are multiple ways to create a virtual environment via virtualenv and venv. To create a virtual environment for different projects using the command given below:
- Using venv
# To create a virtual environment using venv
$ python3 -m venv ENV_NAME# To activate it
$ source ENV_NAME/bin/activate# To deactivate it
$ deactivate
- Using virtualenv
# To install virtualenv
$ sudo pip3 install virtualenv# if it is not available on cli check if it exists in /usr/bin. If not check the /usr/local/bin directory and move it to /usr/bin.# To create a virtual environment using virtualenv
$ python3 -m venv ENV_NAME# To activate it
$ source ENV_NAME/bin/activate# To deactivate it
$ deactivate
9. Install Ipython, to use python on the shell interactively:
$ pythonX -m IPython
Final Thoughts
I hope you enjoyed this story. Please share your feedback about anything that can be improved or I missed. Thank you.