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 .
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 .
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 .
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)
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
Here an example without the placeholders, that might make it easier to understand.
$ pip install git+ssh://git@github.com/pandas-dev/pandas.git
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)
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",
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.
These past couple of months I have used uv, which is a drop-in replacement for pip (among other often used tools in the python eco system) written in Rust.
The biggest selling point is that it significantly speeds up the creation of virtual environments and the installation of packages. Especially if you are recreating virtual environments, because it uses caching. We are talking about being 10 times faster ... or even 100 times faster if the cache is warm.
The list of benefits is much longer than that, but also this is a thing for another article. So, give it a try for now and thank me later.
Conclusion
Let's wrap this thing up.
These were all the ways I have used during my work when it comes to the interplay between git and pip. There might be other ways to install stuff, but this should cover 99% of use-cases.
Have I forgotten your favorite command line trick? Then share it in the comments.
I hope you learned something new with this article and if you are interested in more technical articles about Software Development, then consider a follow.
The above is the detailed content of All you need to know on how to install things with pip. For more information, please follow other related articles on the PHP Chinese website!

There are many methods to connect two lists in Python: 1. Use operators, which are simple but inefficient in large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use the = operator, which is both efficient and readable; 4. Use itertools.chain function, which is memory efficient but requires additional import; 5. Use list parsing, which is elegant but may be too complex. The selection method should be based on the code context and requirements.

There are many ways to merge Python lists: 1. Use operators, which are simple but not memory efficient for large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use itertools.chain, which is suitable for large data sets; 4. Use * operator, merge small to medium-sized lists in one line of code; 5. Use numpy.concatenate, which is suitable for large data sets and scenarios with high performance requirements; 6. Use append method, which is suitable for small lists but is inefficient. When selecting a method, you need to consider the list size and application scenarios.

Compiledlanguagesofferspeedandsecurity,whileinterpretedlanguagesprovideeaseofuseandportability.1)CompiledlanguageslikeC arefasterandsecurebuthavelongerdevelopmentcyclesandplatformdependency.2)InterpretedlanguageslikePythonareeasiertouseandmoreportab

In Python, a for loop is used to traverse iterable objects, and a while loop is used to perform operations repeatedly when the condition is satisfied. 1) For loop example: traverse the list and print the elements. 2) While loop example: guess the number game until you guess it right. Mastering cycle principles and optimization techniques can improve code efficiency and reliability.

To concatenate a list into a string, using the join() method in Python is the best choice. 1) Use the join() method to concatenate the list elements into a string, such as ''.join(my_list). 2) For a list containing numbers, convert map(str, numbers) into a string before concatenating. 3) You can use generator expressions for complex formatting, such as ','.join(f'({fruit})'forfruitinfruits). 4) When processing mixed data types, use map(str, mixed_list) to ensure that all elements can be converted into strings. 5) For large lists, use ''.join(large_li

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
