Home > Software engineering >  Python pip packages on local files, not in venv
Python pip packages on local files, not in venv

Time:12-29

I have a device with Python 3.7 pre-installed, without any pip package.

I created the program on my local machine with some packages in my venv (I have a requirements.txt file) and it works perfectly.

My problem is that now I want to create a directory with my programs and upload it to my device. This doesn't work because I don't have additional packages installed.

My question: Is there a way to export the installed package to a directory in my program files and import it locally and not from venv?

CodePudding user response:

Copy the all the venv modules to some directory and modify PYTHONPATH variable when running your program, append your modules directory path to it.

man python3

   PYTHONPATH
          Augments  the  default  search  path for module files.  The format is the same as the shell's $PATH: one or more directory
          pathnames separated by colons.  Non-existent directories are silently ignored.  The default search  path  is  installation
          dependent, but generally begins with ${prefix}/lib/python<version> (see PYTHONHOME above).  The default search path is al‐
          ways appended to $PYTHONPATH.  If a script argument is given, the directory containing the script is inserted in the  path
          in front of $PYTHONPATH.  The search path can be manipulated from within a Python program as the variable sys.path.

CodePudding user response:

In general, you have the following options to run a python script on another device than the one you developed the script on:

  1. Generate an executable (for example with the package pyinstaller). With that solution, it is not required to have python installed on your device, as everything is embedded in the executable
  2. If you have python installed on the device (like your case), you can just run it on it. However, if you have dependency (from PyPi or Conda), you must also install them on your device
    • If you have access to internet and have your requirements.txt file, you can just run pip install -r requirements.txt
    • If you don't have access to internet, you can either:
      • download the wheel for each package and then ship it to your device
      • just ship to your device the contents of the folders lib and lib64 of your virtual environnement folder .venv of your local machine (I hope you are using one python -m venv .venv) into the virtual environment of your device
  • Related