Python equivalent of npm or rubygems?
Asked Answered
Z

7

75

I've been looking around for a package manager that can be used with python. I want to list project dependencies in a file.

For example ruby uses Gemfile where you can use bundle install.

How can I achieve this in Python?

Zrike answered 24/11, 2012 at 3:10 Comment(0)
P
90

The pip tool is becoming the standard in equivalent of Ruby's gems.

Like distribute, pip uses the PyPI package repository (by default) for resolving and downloading dependencies.

pip can install dependencies from a file listing project dependencies (called requirements.txt by convention):

pip install -r requirements.txt

You can "freeze" the current packages on the Python path using pip as well:

pip freeze > requirements.txt

When used in combination with the virtualenv package, you can reliably create project Python environments with a project's required dependencies.

Plasmo answered 24/11, 2012 at 3:28 Comment(4)
pip freeze created list of all libraries installed in my virtual environment. Is there any way to automatically exclude those that are not necessary for current project to work?Maymaya
@Maymaya There's a -l (or --local) option that may do what your'e asking. pip.pypa.io/en/stable/reference/pip_freezeWrennie
@LXXIII little too late, but thanks anyway ;) I think this particular option deserves to be separate answer. Seems like pretty useful option, which no one knows about.Maymaya
Hey, sorry for this silly question but where does requirements.txt goes? :cDrippy
A
51

Pipenv

(I know it's an old question, and it already has an answer but for anyone coming here looking for a different answer like me.)

I've found a very good equivalent for npm, It's called pipenv. It handles both virtualenv and pip requirements at the same time so it's more like npm.


Simple Use Case

pip install pipenv

then you can make a new virtualenv with third version of python, as well as making a pipfile that will be filled with your projects requirement and other stuff:

pipenv install --three

using your created virtualenv:

pipenv shell

installing a new python package:

pipenv install requests

running your .py file is like:

pipenv run python somefile.py

you can find it's doc here.

Aronson answered 3/9, 2017 at 6:10 Comment(3)
this is the first I heard of pipenv - thanks, this is greatFilberto
pipenv doesn't provide all the features provided by npm. It only manages dependecies.Radicle
poetry and pdm are good alternatives to pipenv.Watercool
M
11

Python uses pip for a package manager. The pip install command has a -r <file> option to install packages from the specified requirements file.

Install command:

pip install -r requirements.txt

Example requirements.txt contents:

Foo >= 1.2
PickyThing <1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1
SomethingWhoseVersionIDontCareAbout

See the Requirements Parsing section of the docs for a full description of the format: https://pip.pypa.io/en/stable/user_guide/#requirements-files

Milkweed answered 9/9, 2013 at 17:21 Comment(0)
W
9

This is how I restrict pip's scope to the current project. It feels like the opposite if you're coming from NodeJS's npm or PHP's composer where you explicitly specify global installations with -g or --global.

If you don't already have virtualenv installed, then install it globally with:

 pip install virtualenv

Each Python project should have its own virtualenv installation. It's easy to set one up, just cd to your project's root and:

 python3 -m virtualenv env  # creates env folder with everything you need

Activate virtualenv:

 source env/bin/activate

Now, any interaction with pip is contained within your project.

Run pip install package_name==version for each of your dependencies. They are installed in ./env/lib/python3.x/site-packages/

When you want to save your project's dependencies to a file, run:

 pip freeze > requirements.txt

You actually don't need -l or --local if you're in an activated project-specific virtualenv (which you should be).

Now, when you want to install your dependencies from requirements.txt, set up your virtualenv, and run:

 pip install -r requirements.txt

That's all.

Wrennie answered 8/6, 2018 at 23:36 Comment(0)
C
3

This is an old question but things are constantly evolving.

Further to the other answer about pipenv. There is also a python package manger called poetry.

There is a detailed comparison between pipenv and poerty here: Feature comparison between npm, pip, pipenv and poetry package managers. It also links the features to common npm features.

Cabinetwork answered 4/10, 2019 at 21:42 Comment(0)
W
2

Here is a comparison of pipenv vs poetry vs pdm: https://dev.to/frostming/a-review-pipenv-vs-poetry-vs-pdm-39b4 The conclusion is that pdm is the winner.

But in my experience, poetry is easier than pdm to integrate with IDEs.

Watercool answered 20/11, 2021 at 15:45 Comment(0)
S
0

pip, virtual environment, requirement_list, Pipenv

I wrote an answer in 2021 and it went into all the related details. A thorough TLDR guide. Think worth sharing here.

Resume (check the answer above)

Python and pip

  • virtual environment
  • requirements file (listing of packages)
  • pip freeze command
  • How to install packages from a requirements file

Virtual environment and why

  • why
  • usage
  • Requirement files
  • Freeze command
  • Installing packages Resume

pipenv (The more close to npm)

  • Installation
  • virtualenv-mapping-caveat
  • Basic usage and comparing to npm
  • environment management with pipenv
  • pipenv run

The answer includes a thorough TLDR guide. I thought it worth sharing here as well

Silage answered 18/9, 2023 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.