简体   繁体   中英

Python : Sending a variable to another script without run

I want change value in Script1.py from Script2.py but Script1 always running. For example;

Script1.py

count = 0
while(count == 0):
    print (count)

Script2.py

// I want change count and break loop here when Script1.py running

Write in Script2.py :

import Script1
Script1.count = 1

You should put the while loop of Script1.py inside a function or Script2.py will never exit the import statement and Script1.count will never be modified.

This lets you modify count in Scripy1.py before the loop starts. If you want to modify it while the loop is running, I don't know how to do it. But it's something you should not do.

you could try putting the variable inside another file were one script writes to the file and another just reads it.

something like this

**script 1. writes to the file**

for x in range(1,100):

    file1 = open("MyFile.txt","w") # open file as write

    file1.write(str(x))

    file1.close() # has to close or it will be appended instead of over writing it 

**script 2. reads the file**

file2=open("MyFile.txt", "r")

if file2.read==Somevalue :

   do something.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM