How to make all my imports in a separete setup.py file then importing this file in main.py?

I'm trying to make a setup script that will install needed python modules on machines that don't have it. so going with the recommended way of installing packages this is my setup.py

import subprocess
import sys
import importlib

required_packages = ["cryptography.fernet", "os", "pip"]
for package in required_packages:
    try:
        importlib.import_module(package)
        print("Imported {} successfully".format(package))
    except ImportError:
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        print("Installed {} successfully".format(package))

and main.py as :

import setup
os.walk(".")   # cause an error

This way I have control about which modules have been installed and which not and then install them. the problem is that importing setup.py in main.py doesn't seem to import these packages in the global namespace of main.py so how can I make these two python files work as intended.



Read more here: https://stackoverflow.com/questions/64869463/how-to-make-all-my-imports-in-a-separete-setup-py-file-then-importing-this-file

Content Attribution

This content was originally published by Khaled Gaber at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: