12

I just created a pipenv environment using pipenv --python 3.9. I then did pipenv shell and started installing packages with pip install. It turns out this doesn't seem to be the usual way of doing things with pipenv. Is there any command I can run to update the Pipfile with all the packages I installed with pip install? I searched but couldn't find anything.

Ariel
  • 3,383
  • 4
  • 43
  • 58

4 Answers4

11

When you have multiple packages you'd like to install, you usually have whats called a requirements.txt file, that contains all the packages you'd like to use for your project.

You can run

$ pipenv run pip freeze > requirements.txt

To generate the requirements file to the current directory you're in, while the virtual environment is active.

Initially you're going to have to install all your packages manually. But after you can run

$ pipenv install -r path/to/requirements.txt

to import all the packages in requirements.txt from the shell/virtual environment.

  • Though this seems to be the accepted convention to install packages in a virtual environment. It does not answer the original question, about how to produce a functional and updated Pipfile from a working venv. I think this is relevant because `pipenv init/shell` will have an erratic behavior unless there's a Pipfile in the root of your project directory. – Sdlion May 30 '22 at 12:58
  • It does answer the original question, in that installing the packages returned by `pip freeze` results in an updated Pipfile with the frozen package versions. Also, pipenv has erratic behavior regardless of the Pipfile. The only way to have a reproducible build is to use the Pipfile.lock. – Ulisse Bordignon Jul 07 '22 at 12:52
6

Instead of running pipenv shell and then pip install <package>, you should simply run pipenv install <package> (outside the virtual environment, from the same location you ran pipenv --python 3.9).

This will install the package in your virtual environment and will automatically update your Pipefile and Pipfile.lock files.

You may skip the Pipfile.lock update by using --skip-lock flag - pipenv install --skip-lock <package>

guy szweigman
  • 514
  • 5
  • 8
1

You can use pipreqs which generates requirements.txt file based on imports.

pip install pipreqs
pipreqs
pipenv install -r requirements.txt
Justin
  • 170
  • 8
1

Do not use pip install in the first place instead use pipenv install

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 17 '23 at 01:48