A Python script that I wrote (one .py file) depends on the requests module, however the target machine does not have requests installed. How can I package the two together? The target machine is a CentOS Linux box.
1 Answer
Use a distutils-based setup script, then install with pip or easy_install.
That way you can specify requests as a dependency and it'll be installed together with your script:
from distutils.core import setup
setup(
# various package metadata fields
install_requires=[
'requests',
],
)
See Declaring Dependencies and the Python Packaging User Guide for more information.
If for whatever reason you cannot use this infrastructure, just unpack the requests tarball next to your script, and add the parent directory of your script to sys.path:
import sys
import os
parentdir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, parentdir)
# rest of your imports go here
import requests
5 Comments
dotancohen
Thanks. Any of the Python tools are inappropriate for this as the Python script is part of an "addon" for phpFox. Therefore requests has to be packaged together in the addon itself. Is there no way to have requests in its own folder and to reference it from there?
dotancohen
For other purposes, the links that you mentioned are great and I will review them further.
Martijn Pieters
@dotancohen: there, stopgap measure added. :-)
dotancohen
Thanks, Martijn. I did something similar with
sys.path.append (I think it was append) but it didn't work. I'm at home, not work, so I'll test this tomorrow but I'm already accepting the answer. Thank you!Martijn Pieters
@dotancohen: I tested it locally when posting with my git working copy of the requests library. :-)