15

So, the title basically covers my question. I've created a project using virtualenv, e.g. I have to

source ./env/bin/activate 

to run my script.

When I try creating an executable using:

pyinstaller --onefile <myscript.py>

None of the virtualenv packages are included; just the ones that are installed globally. I have a requirements.txt file that contains all of the modules I need. Is there a way to have pyinstaller point to that for the needed modules, or is there another way?

Topher Sikorra
  • 161
  • 1
  • 1
  • 5
  • Maybe this helps: https://stackoverflow.com/questions/48629486/how-can-i-create-the-minimum-size-executable-with-pyinstaller – Valentino Mar 18 '19 at 21:57

2 Answers2

19

As Valentino pointed out by looking at How can I create the minimum size executable with pyinstaller?

You have to run PyIntaller from inside the virtual environment:

(venv_test) D:\testenv>pyinstaller
Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47
  • 1
    And your finished file will now be in the virtual environment's "dist" directory if not specified (e.g., "D:/testenv/dist" – Sludge Aug 24 '22 at 17:38
1

How to solve the not importing modules from the virtual environment

The virtual environment saves modules in a different directory than the global module directory. If you are using Windows like me, you can find the modules directory here:

C:\Users\<your username>\.virtualenvs\<your project name>\Lib\site-packages

When you find your virtualenv directory, run this command instead of this simple command(pyinstaller <script>.py):

pyinstaller --paths "C:\Users\<your username>\.virtualenvs\<your project name>\Lib\site-packages" --hidden-import <module name that should be import> <your script name>.py

  • To export just one file you can add this: -F or --onefile
  • As many modules as you can add to be imported by adding more --hidden-import flags and module name

Flag description

--paths: The pyinstaller will search for imports here

--hidden-import: Which modules should be imported by pyinstaller from the path