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.
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.
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.
What's the best package manager for python in your opinion?
The Python equivalent to "npm install" from package.json?
Personally I'd say Poetry is overall the closest match for npm, there are no 1:1 equivalents.
I use it everywhere nowadays because it's just so darn nice. I can give it the version ranges I'd like to use, and Poetry finds the best matches for each dependency that don't conflict with each other, saving the exact versions in a lock file much like npm or Cargo would.
Installing dependencies is as simple as
poetry install
and upgrading them equally so:
poetry upgrade
Of course it's not all roses, but I've found it to be a much better tool than pip alone.
path - How to use pip install python packages locally like npm does - Stack Overflow
Python equivalent of `npm install` for a given directory/project?
Yes. Use pip to make a line-delimited file (usually named "requirements.txt") of the currently installed modules:
pip freeze > requirements.txt
Then on your new machine use pip again to install them all:
pip install -r requirements.txtMore on reddit.com
Videos
Mine is personally uv because it's so fast and I like the way it formats everything as a package. But to be fair, I haven't really tried out any other package managers.
Hello all,
I have a bit of a background in node.js and the npm install feature is quite handy. I've done some googling there seems to be a few ways this is accomplished:
-
Using PIP requirements.txt
-
Using a 3rd party platform like Python Poetry
I'm looking for something simple that's the equivalent of npm install. What's the norm?
Kind regards
Personally I'd say Poetry is overall the closest match for npm, there are no 1:1 equivalents.
I use it everywhere nowadays because it's just so darn nice. I can give it the version ranges I'd like to use, and Poetry finds the best matches for each dependency that don't conflict with each other, saving the exact versions in a lock file much like npm or Cargo would.
Installing dependencies is as simple as
poetry install
and upgrading them equally so:
poetry upgrade
Of course it's not all roses, but I've found it to be a much better tool than pip alone.
I've gotten a lot of mileage by using:
pip install -r requirements.txt
I find that a lot of projects also tend to do this as well. It's a simple text file that contain all of the relevant dependency information.
You can use something like Poetry, but I've found it largely unnecessary for the projects I've worked on (that's not to say that there aren't projects where it is necessary to use something like this).
The nice thing to consider is that pip comes with python whereas Poetry does not, making it so that it would require more setup to get other people up to speed.
This all assumes that you are working within a virtual environment, which is an absolute must for any Python project.
The usual solution to this problem in the Python world is to use virtualenvs or, even better, a wrapper like pipenv. If you install pipenv, you should be able to create a new virtualenv with a simple pipenv install:
[user@host Foo]$ pipenv install
Creating a virtualenv for this
project… ⠋Using base prefix '/usr' New python executable in
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/python3 Also
creating executable in
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/python Installing
setuptools, pip, wheel...done.
Virtualenv location: /home/user/.local/share/virtualenvs/Foo-oXnKEj-P
Creating a Pipfile for this project… Pipfile.lock not found, creating…
Locking [dev-packages] dependencies… Locking [packages] dependencies…
Updated Pipfile.lock (c23e27)! Installing dependencies from
Pipfile.lock (c23e27)… �� ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 —
00:00:00 To activate this project's virtualenv, run the following: $
pipenv shell
Then enter the virtualenv with pipenv shell
[user@host Foo]$ pipenv shell
Spawning environment shell
(/bin/bash). Use 'exit' to leave. source
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/activate
[user@host Foo]$ source /home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/activate
Finally, you can install the packages in your requirements.txt:
(Foo-oXnKEj-P) [user@host Foo]$ pip install -r requirements.txt
Collecting Django==2.0.4 (from -r requirements.txt (line 1)) Using
cached
https://files.pythonhosted.org/packages/89/f9/94c20658f0cdecc2b6607811e2c0bb042408a51f589e5ad0cb0eac3236a1/Django-2.0.4-py3-none-any.whl
Collecting pytz (from Django==2.0.4->-r requirements.txt (line 1))
Using cached
https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl
Installing collected packages: pytz, Django Successfully installed
Django-2.0.4 pytz-2018.4
(Foo-oXnKEj-P) [user@host Foo]$
You can also use poetry with local poetry.toml config
[virtualenvs]
in-project = true
in js/node for any given project you include a package.json file and when the command npm install is run in that directory it will install all of the required libraries/dependencies listed in package.json (as new packages/libraries are installed they're automatically included in package.json).
Is there a python equivalent to this? If I move my project to a new machine, I'd like to install all of the project requirements in a single command, is this possible?
Thanks.