Heim > Backend-Entwicklung > Python-Tutorial > Alles, was Sie wissen müssen, um Dinge mit Pip zu installieren

Alles, was Sie wissen müssen, um Dinge mit Pip zu installieren

PHPz
Freigeben: 2024-08-12 18:46:39
Original
327 Leute haben es durchsucht

All you need to know on how to install things with pip

In this article we are looking at different ways to install code into your virtual environment with pip.

These will increase in complexity, but fret not, I am there for you every step of the way. pats your back

Enough talk! Let's start with something easy.

Installing a local repository

Assume the following situation: You just checked out a repository and want to install the requirements.

This can easily be done by using the following command ... of course after you have created a virtual environment:

$ python -m venv (name of virtual environment)
$ source (name of virtual environment)/bin/activate
$ pip install .
Nach dem Login kopieren

If you are wondering about the install command and the absence of a requirements.txt, I have bad news for you. It is 2024 and you should no longer use a requirements.txt.

This is of course only my own opinion, but all repositories I am working with have a pyproject.toml and I would strongly recommend to use one in every of your projects as well. Viable exception might be sandbox projects and small scripts.

The "why" would be misplaced here, but allow me to give you a sneak peek. Not only does it allow you to define your requirements themselves. You can also define optional dependencies the user can install if needed.

This is especially useful for development tools, that you do not want in the productive application, like testing libraries or formatters.

But this is only the beginning of the list of features. They are also a place for metadata and allow for custom entry points to your application.

Here the install command again:

$ pip install .
Nach dem Login kopieren

Make sure that you are in the folder where the pyproject.toml can be found.
Here a pro-tip, use the following command if you intend to work on the repository you are installing

$ pip install -e .
Nach dem Login kopieren

This performs an editable install also known as "Development Mode" which allows you to iteratively test your code changes without the need to reinstall your project.

What does that mean?

Did it ever happen to you that you changed code in a module you are importing from, but the changes do not seem to take effect?

Try editable installs!

Interlude: Installing from a branch

Quick question: What do you do, if you want to install the version of a specific branch instead of the default branch?

The answer is obvious

$ git checkout (branch name)
Nach dem Login kopieren

and repeat the steps above! Right?

You fool, you just activated my trap card!

See, ever since version 2.23 there is a new kid in town which allows for more intuitive switching of branches and its name is git switch.

git checkout has been deprecated ever since.

So do not expose yourself in front of you colleagues by using out-dated tools. Instead, casually drop a git switch next time you are sharing your screen to let everyone know that you mean business.

Install from a private repository

Brace yourself!

Everything up to this point was mere child's play. Now it is time for some big boy pip usage.

See, everyone can install packages available in a package repository, but only knowing how to install from there will mean that all the gold in private repositories will remain inaccessible to you.

It is also helpful to test your own code before turning it into a package.

If you ever find yourself in such a situation, use this command:

$ pip install git+ssh://git@(your provider)/(owner)/(repo name).git
Nach dem Login kopieren

Here an example without the placeholders, that might make it easier to understand.

$ pip install git+ssh://git@github.com/pandas-dev/pandas.git
Nach dem Login kopieren

Fun fact: everything after the '://' is almost identical to the ssh command generated by git. But notice, instead of the colon used to separate 'github.com' and the owner 'pandas-dev' a slash has to be replaced.

What if you want to install from a branch ... or any other reference for that matter?

Easy! Just add a @(ref) at the end of the command. So it can look something like

$ pip install git+ssh://git@github.com/pandas-dev/pandas.git@1.5.x (branch)
$ pip install git+ssh://git@github.com/pandas-dev/pandas.git@v2.2.2 (tag)
Nach dem Login kopieren

Private Repos and the pyproject.toml

But what if it is not enough to install packages from the command line? What if also your build pipeline should install from a private repository?

Hopefully, you agree that adding individual pip install statements to your pipeline is out of the question.

So instead, let me show you what to add to the dependencies section of the pyproject.toml. You will see, that it is very similar to the previous command:

"pandas@git+ssh://git@github.com/pandas-dev/pandas.git@1.5.x",
Nach dem Login kopieren

With this added, run again pip install -e ..

Congrats! You have just installed an outdated version of pandas into your environment. You might want to repeat that with the actual package you need.

Now make it fast

Since you stuck with me until this point, I will throw in a bonus tool recommendation.

In den letzten paar Monaten habe ich uv verwendet, einen in Rust geschriebenen Ersatz für pip (neben anderen häufig verwendeten Tools im Python-Ökosystem).

Das größte Verkaufsargument ist, dass es die Erstellung virtueller Umgebungen und die Installation von Paketen erheblich beschleunigt. Vor allem, wenn Sie virtuelle Umgebungen neu erstellen, da Caching verwendet wird. Wir sprechen davon, 10-mal schneller zu sein ... oder sogar 100-mal schneller, wenn der Cache warm ist.

Die Liste der Vorteile ist viel länger, aber auch das ist Thema für einen anderen Artikel. Probieren Sie es also erst einmal aus und danken Sie mir später.

Abschluss

Lassen Sie uns die Sache abschließen.

Das waren alle Methoden, die ich während meiner Arbeit verwendet habe, wenn es um das Zusammenspiel zwischen Git und Pip geht. Es gibt möglicherweise andere Möglichkeiten, Dinge zu installieren, aber diese sollten 99 % der Anwendungsfälle abdecken.

Habe ich deinen Lieblingsbefehlszeilentrick vergessen? Dann teile es in den Kommentaren.

Ich hoffe, Sie haben mit diesem Artikel etwas Neues gelernt und wenn Sie an weiteren technischen Artikeln über Softwareentwicklung interessiert sind, dann denken Sie darüber nach, ihm zu folgen.

Das obige ist der detaillierte Inhalt vonAlles, was Sie wissen müssen, um Dinge mit Pip zu installieren. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage