Machine Learning

The perfect guide to the dependency of Python

When reading the Python, many beginners focus on their language and libraries while ignoring the visual areas. As a result, Python project management can be dirty: Depending on different projects can have conflicting versions, which results in compliance issues.

Or I'm studying Python, no one emphasized the importance of visible places, which I now find. They are a very useful tool for dividing different projects to each other.

In this article, I will explain how the existing locations work, they provide several examples, and share practical behavioral instructions.

Problem

Imagine that you have two Python projects in your laptop, each available in a different identifier. You see that you need to install the latest version of Library A for the first project. Later, you switch to the second project and try to install the library B.

Here is the problem: The Library B depends on the library A, but requires a different version than you have installed before.

Since you have not yet used any reliance management tool, all depends on the worldwide installation on your computer. Due to different types of Library A, you experience an error when trying to install Library B.

Solution

To prevent such matters, material areas are used. The idea to share the final space of each Python project. Each storage will contain all the relevant dependency on a certain project in a remote manner.

Clearly, when we download the same library a two-sided library, library will be downloaded twice – and each environment. In addition, types of library may vary between areas because each environment is completely divided and does not meet others.

Now that the behind motivation is using visible areas is clear, let us check how to build a Spython.

Locations in Python

It is recommended to create a visual area in the project root root. Nature is created using the following command in the Sygin:

python -m venv 

By a meeting, often called The in-line gardenTherefore the commandment is:

python -m venv venv

As a result, this command creates a directory called The in-line gardenwhich contains the physical environment itself. It is also possible to get in the direction, but in many cases, it is not very helpful, as The in-line garden The directory mainly contains system documents that are not intended for specific use.

To use visual nature, use the following command:

source venv/bin/activate

When the environment is activated, we can include a project dependency. As long as The in-line garden activated, any relay entries will only be that area.

To activate the visual nature, type:

deactivate

When the environment is activated, the terminal returns to its normal state. For example, you can switch to another project and use its nature there.

Dependence management

Installing libraries

Before installing any reliance, it is recommended to activate the visual area to ensure that the libraries of the Information Officers are of the same project. This helps to avoid conflict of the global version.

The most widely used command to be dependent on PIP. Compared to other means, pate Accurate and easy to use.

To add a library, type:

pip install 

To the examinations below instead of I will write pandas (a very used data library).

So, for example, if we wanted to download the latest version of Pandas, you should have typed to:

pip install pandas

In some cases, we may need to file a copy of the library. pate Provides simple syntax to do that:

pip install pandas==2.1.4 # install pandas of version 2.1.4
pip install pandas>=2.1.4 # install pandas of version 2.1.4 or higher
pip install pandas<2.1.4 # install pandas of version less than 2.1.4
pip install pandas>=2.1.2,<2.2.4 # installs the latest version available between 2.1.2 and 2.2.4 

To view reliance information

If you are interested in relying on something you entered, an easy way to get more details about using pip show Command:

pip show pandas

For example, command to for example will issue the following information:

An example of the issue of the Pip Show command

Deleting Dependence

To remove reliance on a visual area, use the following command:

pip uninstall pandas

After issuing this command, all files related to the prescribed library will be deleted, so it releases the disk space. However, if you are conducting the Python system that entries the library again, you will meet a more person.

File with the required

A common practice when manage to depend on creating a Requirements.txt File containing a list of all depends on the project and their versions. Here is an example of what you may look like:

fastapi==0.115.5
pydantic==2.10.1
PyYAML==6.0.2
requests==2.32.3
scikit-learn==1.5.2
scipy==1.14.1
seaborn==0.13.2
streamlit==1.40.2
torch==2.5.1
torchvision==0.20.1
tornado==6.4.2
tqdm==4.67.1
urllib3==2.2.3
uvicorn==0.32.1
yolo==0.3.2

Well, every time you are using pip install command, should add a line corresponding to Requirements.txt File tracking All libraries used in this work.

However, if you forget to do that, there is still another way: pip freeze Commof all discharging all depends on the project. However, pip freeze You can become a Verbose, often adds more words to the library-depending library to the project.

pip freeze > requirements.txt

Given this, it is a good practice to add the needs included and their types of requirements.txt.

Whenever you combine the Python project, it is expected that the requirements file.txt has already existed in the storage area. To include all leaning in this file, using pip install Command and the R-R-flag followed by the requirements of the requirements.

pip install -r requirements.txt

On the other hand, whenever you work in the Python project, you must create a requirement file.txt so that other participants can easily install the required dependency.

.gitignore

When working on verbsion control programs, material areas should never be drawn to GIT! Instead, it should be spoken in the .gi'tignore file.

Visible materials are often very big, and if there is a requirement of the requirements.txt there is no problem, there should be no problem to download all required dependence.

Store

In this article, we look to the most important concept of visible sites. By distinguishing the relate dependency of different projects, allow simple management of many Python projects.

All photos are a writer unless noted in another way.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button