How to automatically install required packages from a python script as necessary?
How to automatically install required packages from a python script as necessary?
Let's assume that your Python script is example.py
:
import osimport timeimport sysimport fnmatchimport requestsimport urllib.requestfrom bs4 import BeautifulSoupfrom multiprocessing.dummy import Pool as ThreadPool print('test')
You can use pipreqs to automatically generate a requirements.txt
file based on the import
statements that the Python script(s) contain. To use pipreqs
, assuming that you are in the directory where example.py
is located:
pip install pipreqspipreqs .
It will generate the following requirements.txt
file:
requests==2.23.0beautifulsoup4==4.9.1
which you can install with:
pip install -r requirements.txt
The best way I know is, create a file requirements.txt
list out all the packages name in it and install it using pip
pip install -r requirements.txt
Example requirements.txt
:
BeautifulSoup==3.2.0Django==1.3Fabric==1.2.0Jinja2==2.5.5PyYAML==3.09Pygments==1.4SQLAlchemy==0.7.1South==0.7.3amqplib==0.6.1anyjson==0.3...
You can use setuptools to install dependencies automatically when you install your custom project on a new machine. Requirements file works just fine if all you want to do is to install a few PyPI
packages.
Here is a nice comparison between the two. From the same link you can see that if your project has two dependent packages A
and B
, all you have to include in your setp.py file is a line
install_requires=[ 'A', 'B']
Of course, setuptools
can do much more. You can include setups for external libraries (say C files), non PyPI dependencies, etc. The documentation gives a detailed overview on installing dependencies. There is also a really good tutorial on getting started with python packaging.
From their example, a typical setup.py file would look like this.
from setuptools import setupsetup(name='funniest', version='0.1', description='The funniest joke in the world', url='http://github.com/storborg/funniest', author='Flying Circus', author_email='flyingcircus@example.com', license='MIT', packages=['funniest'], install_requires=[ 'markdown', ], zip_safe=False)
In conclusion, it is so simple to get started with setuptools
. This package can make it fairly easy to migrate your code to a new machine.