Search code examples
pythonpython-importpython-modulepython-importlib

Trying to import modules using another script


I am trying to run a basic setup.py script that will import modules for me, at the start of another, more complex script.

for instance, setup.py script looks like this:

import requests 
import pandas as pd

modules_imported = True 

My main script then starts off like so:

import os
setup_path = ("/home/solebay/My\\ Project\\ Name/setup.py")

if 'modules_imported' not in globals():
    os.system('python3 ' + setup_path)
    print("modules imported!")

However, the modules are not imported and running it repeatedly causes the "modules imported!" message to print (evidently proving the statement is untrue).

Is the script not truly running or are the modules not being installed permanently?

Adding print("Success!") to startup.py has revealed that the main script does not return the contents of the print command. In what sense is the script being executed if any?


Troubleshooting in response to the suggestion by BartoszKP, and with further assistance from Maurice Meyer in another question I posed, I attempted the following:

import importlib
if 'modules_imported' not in globals():

    start_up_script = importlib.util.spec_from_file_location(
        name="mod_name",
        location="/home/solebay/My Project Name/setup.py"
        )    
    my_mod = importlib.util.module_from_spec(start_up_script)
    start_up_script.loader.exec_module(my_mod)

Despite type(start_up_script) containing something that looks correct: _frozen_importlib.ModuleSpec, modules_imported is never set to True as per my startup.py script.

Additionally the following test fails:

test_data = {'C1':  ['AA', 'AB'], 'C2': ['BA', 'BB']}
test_df = pd.DataFrame (data, columns = ['C1','C2'])

NameError: name 'pd' is not defined
#Clearly the modules aren't loading into my env.

Solution

  • When you run os.system with python3 you are just launching a new, completely independent Python interpreter with its own universe. This doesn't affect your current execution environment.

    It seems what you want is something more like this:

    spec = importlib.util.spec_from_file_location('setup', '/home/solebay/My\\ Project\\ Name/')
    module = importlib.util.module_from_spec(spec)
    

    More details in the documentation of the importlib module.