Search code examples
pythonpythonpathegg

Using Python libraries without installing them globally


I have a python script that I want to deploy to various PCs to run tests, and I need to use the third-party library pysnmp. Since I don't want to have to manually install pysnmp on every target PC, I figured I could just put pysnmp (and related) eggs in an eggs/ directory and just append that to the python path before importing:

import sys
sys.path.append("./eggs/pysnmp-4.2.5-py3.4.egg")
sys.path.append("./eggs/pyasn1-0.1.7-py3.3.egg")
from pysnmp.entity.rfc3413.oneliner import cmdgen

This works as expected as long as I execute the script in the directory containing eggs/, however it seems rather manual and messy. Is there a portable way to tell python to just search through [path to module]/eggs/ anytime I want to import?


Solution

  • The solution I use is to get the directory of the script (eg: os.path.dirname(__file__)), and then use the absolute path of that when adding to sys.path.