Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve pip requirements (freeze) within Python?

Tags:

python

pip

freeze

I posted this question on the git issue tracker: https://github.com/pypa/pip/issues/2969

Can we have some manner of calling pip freeze/list within python, i.e. not a shell context?

I want to be able to import pip and do something like requirements = pip.freeze(). Calling pip.main(['freeze']) writes to stdout, doesn't return str values.

like image 783
Rex Hardin Avatar asked Jul 08 '15 21:07

Rex Hardin


People also ask

Does pip freeze show dependencies?

pip freeze might seem very useful initially but it can mess up your project because of the following reasons: It dumps all the libraries installed in your project including dependencies and sub-dependencies in the requirements. txt file. It still misses out on the libraries that are not installed using pip.

How do I find my pip list?

For the full list of pip options, simply run pip --help in your terminal and the command will return the usage information.


1 Answers

There's a pip.operation.freeze in newer releases (>1.x):

try:
    from pip._internal.operations import freeze
except ImportError:  # pip < 10.0
    from pip.operations import freeze

x = freeze.freeze()
for p in x:
    print p

Output is as expected:

amqp==1.4.6
anyjson==0.3.3
billiard==3.3.0.20
defusedxml==0.4.1
Django==1.8.1
django-picklefield==0.3.1
docutils==0.12
... etc

like image 57
Rex Hardin Avatar answered Sep 22 '22 15:09

Rex Hardin