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
Tiel
  • 87
  • 1
  • 9

1 Answers1

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
enzo
  • 9,861
  • 3
  • 15
  • 38
  • 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 Jul 25 '21 at 09:45
  • @Tiel I've updated my answer, see if this helps to solve your problem. – enzo Jul 25 '21 at 09:56
  • 1
    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. – Tiel Jul 25 '21 at 10:19
  • 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. – Tiel Jul 25 '21 at 10:51
  • I'm sorry I forgot to mention you in my last two comments immediately above. – Tiel Jul 25 '21 at 11:08
  • Please see the following question I just posted on StackOverflow... https://stackoverflow.com/questions/68518107/im-trying-to-create-a-simple-text-expander-using-python – Tiel Jul 25 '21 at 11:30