ElessarGR
ElessarGR

Reputation: 35

Pyinstaller create an executable with external libraries

I am trying to create an executable file (exe) for a Python script that I written in PyCharm. When I run the script from the PyCharm is working fine but when I try to run it as an individual .py or try to run the exe I am getting an error.

The issue I believe is with the "from infi.devicemanager import DeviceManager" library.

from infi.devicemanager import DeviceManager # Used for the retrievement of Device manager information
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices # Get all the devices to a list called devs
for d in devs:
    print (d.description)

I get following error:

PS C:\Python> python.exe .\tests.py
Traceback (most recent call last):
  File ".\tests.py", line 1, in <module>
    import infi.devicemanager # Used for the retrievement of Device manager information
ModuleNotFoundError: No module named 'infi'

PS C:\Python> pyinstaller -F .\tests.py

PS C:\Python\dist> .\tests.exe
Traceback (most recent call last):
  File "tests.py", line 1, in <module>
ModuleNotFoundError: No module named 'infi'
[15072] Failed to execute script tests

Is there a way to include this library to the exe file so anyone without python can run this script? I am open to suggestions.

Thank you in advance!

=== UPDATE === Full script can be found here https://github.com/elessargr/k9-serial

Upvotes: 0

Views: 10784

Answers (1)

buran
buran

Reputation: 14218

My answer assumes that the interpreter that has infi.devicemanager is C:\Python\venv\Scripts\python.exe as stated by OP in the comments

So there is virtual environment named venv. You need to activate the virtual environment and install PyInstaller in it.

C:\Python>venv\Scripts\activate
(venv)C:\Python>

Note (venv) in front of your shell - that means virtual environment venv is activated Now install PyInstaller

(venv)C:\Python>pip install pyinstaller
-- here it will install pyinstaller --

now you can test that both pyinstaller and infi.devicemanager are installed in venv

(venv)C:\Python>pip list

it should list both packages among some others. now

(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager
--here it will create the exe --

if I was right it should work now

EDIT: It looks like infi is using __import__(pkg_resources), so the command should be

(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager --hiden-import=pkg_resources

Upvotes: 1

Related Questions