Search code examples
pythonbatch-filepyinstaller

Run pyinstaller within virtual environment from batch file


What I am trying to do is create a batch file to create a virtual environment, install requirements, and then use pyinstaller to convert the python file to an executable.

I have a folder (.venv) with the following folders/files:

  • upx (folder with the upx .exe)

In the parent folder there is:

  • fileName.py
  • icon.ico
  • compileEXE.bat

The Code:

copy fileName.py .venv
copy icon.ico .venv

cd .venv

@echo off
Set "out=."
(
    Echo;pytube==15.0.0
    Echo;moviepy==1.0.3
    Echo;keyboard==0.13.5
) > "%out%\requirements.txt"

echo;requirements.txt created

@echo on
python -m venv fileName

cmd /k "pip install -r requirements.txt"

fileName\Scripts\activate.bat

pyinstaller fileName.py -F --icon=icon.ico --upx-dir upx

I have tried changing the last line, with no luck, to:

start /b "" "python.exe" "pyinstaller fileName.py -F --icon=icon.ico --upx-dir upx

cmd /k "pyinstaller fileName.py -F --icon=icon.ico --upx-dir upx

Expected Outcome:

  • fileName.exe is created in the venv/dist folder
  • virtual environment fileName with dependencies is created
  • icon.ico, fileName.py is moved
  • requirements.txt is created

Actual Outcome:

  • virtual environment fileName with dependencies is created
  • icon.ico, fileName.py is moved
  • requirements.txt is created

I can run the last line in the terminal and pyinstaller will work.

How do I execute this last line within the virtual environment from the Batch file?

Solution

  • Thank You Alexander and Mofi for the help.

    Including pyinstaller in the requirements.txt file reduced the file size by 50% and the call function worked perfectly. This is my final code:

    copy fileName.py .venv
    copy icon.ico .venv
    
    cd .venv
    
    @echo off
    Set out="."
    (
        Echo;pytube==15.0.0
        Echo;moviepy==1.0.3
        Echo;keyboard==0.13.5
        Echo;pyinstaller==5.10.1
    ) > "%out%\requirements.txt"
    
    echo;requirements.txt created
    
    @echo on
    python -m venv fileName
    
    pip install -r requirements.txt
    
    call fileName\Scripts\activate.bat
    
    pyinstaller fileName.py -F --icon=icon.ico --upx-dir upx