Search code examples
pythonpipvirtualenvpyinstaller

pip installed Pyinstaller in global site-packages instead of venv


I've created virtual environment using following command:

py -m venv pygui

Then I've activated it in CMD prompt:

cd pygui/Scripts
activate.bat

However when I've tried to install new libraries with pip I don't see it in the list.

pip install pyinstaller
pip list

The only library I can see is: pip 23.2.1 enter image description here

Even if I'm trying to install it outside the environment, as:

py -m pip install pyinstaller --upgrade

I'm not able to call it, as I get the following error: 'pyinstaller' is not recognized as an internal or external command,
operable program or batch file.

I've checked "path" in environment variables and looks correct to me.

C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts
C:\Users\USER\AppData\Local\Programs\Python\Python312
C:\Users\USER\AppData\Local\Programs\Python\Launcher\
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
C:\Users\USER\AppData\Local\Programs\Microsoft VS Code\bin

Solution

  • Following steps fixed the issue:

    [Set-ExecutionPolicy] Based on answer provided by Rahul Chauhan in virtualenv activate does not work, update the execution policy.

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUs
    

    [Update pip.ini] Then according to Uralbi in pip installing in global site-packages instead of virtualenv, update content in "pip.ini" in the following directory: "C:\Users\user_name\AppData\Roaming\pip\pip.ini".

    [global]
    trusted-host = pypi.org files.pythonhosted.org pypi.python.org
    default-timeout = 60
    respect-virtualenv = true
    download-cache = /tmp
    log-file = /tmp/pip-log.txt
    

    [.venv] Finally create .venv, activate it and convert .py to .exe.

    python -m venv .venv
    .venv\Scripts\activate.ps1
    pip install -r requirements.txt
    python -m PyInstaller --onefile -w 'main.py'
    

    [pyinstaller error]: Note in order to avoid this *win32ctypes.pywin32.pywintypes.error when using pyinstaller in VS Code - Possible Virus/Trojan? error:

    File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyInstaller\utils\win32\icon.py", line 143, in CopyIcons_FromIco
        hdst = win32api.BeginUpdateResource(dstpath, 0)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32ctypes\pywin32\win32api.py", line 208, in BeginUpdateResource
        with _pywin32error():
      File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 155, in __exit__
        self.gen.throw(typ, value, traceback)
      File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
        raise error(exception.winerror, exception.function, exception.strerror)
    **win32ctypes.pywin32.pywintypes.error: (225, 'BeginUpdateResourceW', 'Operation did not complete successfully because the file contains a virus or potentially unwanted software.')**
    

    You can use older release (5.13.2) of PyInstaller:

    pip install pyinstaller==5.13.2
    

    If anyone else have alternative or better solutions, please let me know.