简体   繁体   中英

Packaging single Python module with dependencies

I have a single Python3 .py module with a few dependencies (lockfile, python-daemon). Is there a simple way to package this with its' dependencies so that users do not need to download and install the other modules? An all included install is what I am trying to do.

I tried looking at setuptools, distribute, and distutils and ended up even more confused than when I started.

The simplest way I see often used is to put all your dependencies in a single file (usually named requirements.txt ) and then you ask user to run the following command:

pip install -r requirements.txt

And here is an example for the content of the file ( https://github.com/cenkalti/pypi-notifier/blob/master/requirements.txt ):

Flask==0.10.1
Flask-Cache==0.12
Flask-SQLAlchemy==1.0
Flask-Script==0.5.3
GitHub-Flask==0.3.4
Jinja2==2.7
MarkupSafe==0.18
SQLAlchemy==0.8.2
...

cx_Freeze应该可以满足您的需求。

You could quite easily do this with something simple like a .zip file containing all the files; so long as all the files are exported to the same directory then they should all work! The downfall is if there are lots of dependancies for the modules, ie they have extra folders you would need to find.

I also think a fair amount of people/companies write their own packaging systems so that all the modules are in 1 .py file that opens in the console and exports everything to it's correct place. This would require a fair amount of work though so you may to try and find one prebuilt. I've gone down this method and it didn't prove too taxing unitl I had to unzip .zips with files in...

As another solution you could try PyExe (I think it's called that) to export everything to a single .exe file (Windows only though)

I personally havn't used setuptools, distribute or distutils so can't comment on those unfortunately.

1 other thing to bear in mind is the licences for each module, some may not be allowed to be redistributed so check first!

py2exe is fine, but will limit you to Windows.

Best way to do this without limiting your audience and following generally accepted best practices is to create a requirements.txt and setup.py file and then upload your project to github . See https://github.com/sourcegraph/python-deps as a reference. The requirements.txt lists the dependencies in a simple, easy-to-read format and you specify the commands and library modules your project installs using the scripts and py_modules options in setup.py.

Assuming your git repository is at github.com/foo/bar, your users can then do pip install git+https://github.com/foo/bar .

Read official Python packaging tutorial .

  • You create a Python package from your module with setup.py

  • In setup.py you can declare what other Python packages must be installed as dependencies

  • Furthermore you can narrow down application specific dependency version with requirements.txt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM