Tech/HowTo/Python Portable Environment

From lathama
< Tech‎ | HowTo
Jump to navigation Jump to search

When you want an environment that is more portable that virtualenv then try adding all you module wheels (*.whl) to ./libraries like:

Special note that "pip download" and "pip install" act differently. Use "pip install" in the case there are no wheels for the library.
pip3 wheel -r requirements.txt -w ./libraries

Early in your imports you can add the *.whl to the path and Python will see them as Zip files and import them directly.

import os
import sys

# Assuming
# pip3 wheel -r requirements.txt -w ./libraries
import glob
for item in glob.glob('./libraries/*.whl'):
    sys.path.insert(1, item)

# To test the imports try using:
# sys.path.pop()
# to trim off the path of dist-packages and confirm with
# print(sys.path)

import numpy

to your code to add the local ./libraries directory to the front of the Python search path. In this example my requirements.txt has:

numpy