0

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 1

5

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
Sign up to request clarification or add additional context in comments.

5 Comments

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?
For other purposes, the links that you mentioned are great and I will review them further.
@dotancohen: there, stopgap measure added. :-)
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!
@dotancohen: I tested it locally when posting with my git working copy of the requests library. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.