0

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.
asked Dec 7, 2020 at 0:05
0

1 Answer 1

2

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.

answered Dec 7, 2020 at 0:26
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.