dotancohen
dotancohen

Reputation: 31511

Include 'requests' module with Python script

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.

Upvotes: 0

Views: 673

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124110

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

Upvotes: 5

Related Questions