I wrote this code to test some imports:
import fnmatch
import os
import psutil
import pygetwindow as window
from time import sleep
import win32api
import PySimpleGUI as pys
import pyautogui as py
from time import sleep
import webbrowser
import winsound
import importlib.util
from random import randint
from datetime import date
import locale
layout = [
[pys.Text(f'Complete =)', size=(25, 0))],
]
jan = pys.Window('Test', layout=layout, finalize=True)
jan.read()
I then made an executable using freeze
, and sometimes the following error appears:
ModuleNotFoundError: No module named: (lib)
It's always a different (lib)
. I tried to run pip install (lib)
for each (lib)
but that didn't work.
Is there some way to check if some (lib)
is installed and if it isn't, automatically download that (lib)
in code?
When you say "making executable using freeze
", I think you are referring to a requirements.txt
file, which is generated by doing pip freeze> requirements.txt
on the command line (and don't forget to remove the unnecessary imports).
You can download all the necessary libraries by doing
pip install -r requirements.txt
To check if a library is installed and automatically install it, you check by using import <packagename>
import sys
import subprocess
try:
import <packagename>
except Exception as e:
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', '<packagename>'])