0

Currently I have....

file_1.py
x = "Carrots"
print (x)

file_2.py
x = "Spinach"
print (x)

file_3.py
x = "Peas"
print (x)

Now let's say I wanted to change print (x) to another command. In that case I would need to change file_1.py, and file_2.py, and file_3.py. I would prefer to make changes to one file, which I am calling main_file.py to accomplish the same result.

Therefore, I want to create something like...

python3 file_1.py | main_file.py

where...

file_1.py
x = "Carrots"

and...

main_file.py
print (x)

or something like...

file_1.py
x = "Carrots"
send x to main_file.py

file_2.py
x = "Spinach"
send x to main_file.py

file_3.py
x = "Peas"
send x to main_file.py

where main_file.py contains a command such as print (x).

I have reviewed...

  1. Python: Sending a variable to another script
  2. Python pass a variable to another script
  3. Pass variable between python scripts

1 Answer 1

0

In your main_file.py:

def action(x):
    print(x)

In your file_1.py (must be in the same directory as main.py):

import main_file

x = "Carrot"
main_file.action(x)

Now let's say I had a directory with multiple sub-directories. What would be the best method to have file_1.py, file_2.py, file_3.py and so on (up to, say, several hundred files in perhaps a half a dozen sub-directories) all "find" main_file.py without hard coding a path to main_file.py?

You can order your project structure to something like that:

p
├───s1
│   └───file_1.py
├───s2
│   ├───s21
│   │   └───file_3.py
│   └───file_2.py
└───main_file.py

Then, in either file_1.py, file_2.py or file_3.py, you can import main_file at the top and use as the above snippet. To execute a particular file (let's say, file_3.py), you can change your current working directory to p using

cd p

and run

python -m s2.s21.file3
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! That worked perfectly. Now let's say I had a directory with multiple sub-directories. What would be the best method to have file_1.py, file_2.py, file_3.py and so on (up to, say, several hundred files in perhaps a half a dozen sub-directories) all "find" main_file.py without hard coding a path to main_file.py?
@Tiel I've updated my answer, see if this helps to solve your problem.
Thank you for your detailed explanation. I appreciate it. I'm going to open another question within the next half hour or so which explains what I am actually trying to do so that you can see "the big picture". Then I will post a link to that question here.
I just tried to post another question on StackOverflow but received the following error message, "You can only post once every 90 minutes." I intend to post the aforementioned "another question" within the next 24 hours.
I'm sorry I forgot to mention you in my last two comments immediately above.
|

Your Answer

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.