Python Installation and Virtual Environments
Python Source Installation
First, Download source files and perform configurations. enable-optimizations is a commonly selected option for source installation.
./configure --prefix=/path-to-python/Python-xxx --enable-optimizations # use Python-xxx for naming convention
Note: Use ./configure --help less to check detailed configurations.
Then, build Python source with make and install the binary after the building with make install:
$ make -j$(nproc) # use all cpus for compile
$ sudo make install # add sudo for admin install if python-path is set to /opt or /usr
Next, add the Python executable path in .bashrc and source it:
$ export PATH=$PATH:/path-to-python/Python-xxx
$ source .bashrc
User Install vs Admin Install
In Linux environments, Python can be installed system-wide (in the directory /opt or /usr) so that every user can use it. The same aplies to Python packages. We need sudo to install packages (e.g., virtualenv) in the Python directory, which is usually in /opt/python3/lib/dist-packages or /usr/python3/lib/dist-packages.
$ sudo /path-to-python/Python-xxx/bin/pip3 install virtualenv
User installation install the Python package to the current user’s directory, which is usually /home/user_name/.local/lib directory. User installation can be performed without sudo or add --user option.
$ python3.x -m pip install --user virtualenv
Python Virtual Environments
Python virtual environments provide separate working spaces for the projects that requires different configurations. A virtual environment is created on top of an existing Python installation, known as the “base” Python. We can install Python packages that are isolated from the packages in the base environment. Many Python packages can provide virtual environments. All of them can be installed by pip command:
virtualenv.pyenv.pipenv.- Standard Python library
venv.
All packages can be installed by pip command. The venv module is shipped with Python distributions and does not require extra installation.
Differences
We refer to the post What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?.
Since Python 3.3, the venv module, which creates lightweight virtual environments, has been integrated into Python official distributions. The venv module contains partial features of the virtualenv module.
$ python3.x -m venv myvenv # create myvenv environment in the current directory using venv module
virtualenv offers more features than venv. It can be installed via pip:
$ python3.x -m pip install --user virtualenv
$ python3.x -m virtualenv env_name # create env_name environment in the current directory
$ source env_name/bin/activate