The Linux operating system has become very popular among admins and developers due to a number of advantages, such as free distribution, availability of open-source code, low requirements to computing resources.
The Python interpreter is preinstalled in most Linux distributions. According to the TIOBE ranking, python is currently the most cited programming language in search queries. It has a low entry threshold, but it has a wide range of uses.
Using python and Linux “in tandem” can make your life a lot easier. I will talk about how to configure python in Linux to suit the needs of your project.
Defining a Virtual Environment
A virtual environment (or virtual environment) is an isolated environment for a project. It is a “sandbox” where an application runs with its own versions of libraries whose updates and changes will not affect other applications using the same libraries. Thus, the use of virtual environments avoids version conflicts.
The virtual environment with all the necessary settings can be “transferred” along with the application. This makes it easier for another developer to work with your project.
Also, libraries which are needed only in one project can be loaded into the virtual environment and thus do not clog up the global environment.
Checking the python version
As mentioned earlier, python is preinstalled in most Linux distributions. I used Ubuntu version 20.04. You can check the current version of python by using the command: python3 -V.
Updating packages
First I will describe how to work with python libraries on Linux.
The Advanced Package Tool (apt) is a package manager which allows to do different manipulations with packages: install, remove, upgrade, search, download without installing them. All dependencies will be resolved automatically.
A package is an archive containing binary and configuration files, information about where to put them in the file system and a list of installation steps. In Linux python libraries are packages.
Linux has a list of repositories from which packages are installed. The list is stored in the text file /etc/apt/sources.list, and in the directory /etc/apt/sources.list.d/. When you run apt update, apt accesses the list of repositories and from each repository in the list retrieves information about the packages in it. All this information is stored in the operating system.
If a new version of a library is released, apt won’t know about it until the information in the repositories is updated. Therefore if you install a package without updating it, the version of the package that is currently installed will be installed.
To update packages, the following two commands must be run.
The first command: sudo apt update.
The second command: sudo apt -y upgrade.
The -y flag in the command denotes automatic confirmation of installation requests.
Installing the venv package
To work with the virtual environment in Linux you need to install the venv package with the command sudo apt install python3-venv.
Creating a virtual environment in Linux
You can create a virtual environment with the command python3 -m venv my_venv.
My_venv is the name of the virtual environment.
The above command creates a directory named “my_venv” (as well as parent directories that don’t already exist) containing the pip package manager, interpreter, scripts and libraries.
You can use the ls -la command to see the files in the current directory.
If you want to create an environment folder in a specific directory, you must specify the path to the folder instead of the environment name. For example, python3 -m venv ~/my_venv.
The pyvenv.cfg file contains a key that will point to the version of Python for which this command is run.
The bin directory contains a copy/symbolic reference of the Python binaries.
The include directory includes the C headers that compile the Python packages.
The share directory includes python wheels. Python wheels is a ready-made package format for Python that helps speed up software development by reducing the number of compilation operations.
The lib directory in the virtual environment has the same structure as the lib directory in the global environment. And it contains a site-packages folder where the libraries are installed.
Activating the virtual environment.
The virtual environment is created. You have to activate it to start using it.
To start using this virtual environment you have to activate it by running the script called activate:
After activation the console line will be prefixed with the name of the environment.
You can check the version of python.
It is also possible to see a list of installed libraries in the environment.
Installing libraries inside the virtual environment
I will try to install the library inside the environment.
After activation, all libraries will be installed in this virtual environment.
To check how the library is installed, you can try importing it.
If there were no errors when importing, it means that the installation of the library was successful.
Sharing a virtual environment
If you want others to be able to run your project code on their computers, they must have the same versions of the libraries as you do.
To do this, I will create a file with all the libraries and their versions.
Run the following command:
The requirements.txt file contains all the libraries (with their versions) that are installed in this environment.
You can install all these libraries by running a single command in the terminal: pip install -r requirements.txt.
After successful installation of libraries, another person can run your project on his computer.
Deactivating virtual environment
The command deactivate can be used to leave a virtual environment.
In this post I have discussed the topic of virtual environments in Linux. This material will allow you to be more aware of the development process and will make it easier to support your projects.
More Stories
Top 13 Benefits of Implementing FHIR in Healthcare Systems
10 Common Challenges in FHIR Implementation
Tips for Starting a Software Development Company