1

I am completely new to python and I'm just trying it out. Something is confusing me for hours until I finally made this little test.

I have 2 scripts, a.py and b.py

#a.py 
num = 3 

#b.py 
import a 

print(a.num)

When b.py is ran, this prints 3. But if I change the value of num to any other number, the output is still 3.

How can I resave / update my script files?

1
  • Yes you can save your files - you will have to do this before running your scripts again. Commented Jul 18, 2013 at 8:12

4 Answers 4

1

Python will only read the module file on the first time the module is imported. So what you are editing is still the old version of the imported objects. If you want to reload a module, you can use imp.reload. For more clarification, you can read When I edit an imported module and reimport it, the changes don’t show up. Why does this happen?.

Sign up to request clarification or add additional context in comments.

3 Comments

So during the development of my project, I have to include imp.reload on the imported modules?? and remove them when the project is finished? This is a hassle.
If you want to reload, you have to. Or you have to terminate your current script, update the imported module, and run the script again. But I think the num should be a fixed number, you don't have to change it in file but change it when you need to in logic.
The above code is just a sample. I'm talking about developing real projects. But never mind.
1

To reload a module, use imp.reload() from the imp module. See http://docs.python.org/3/library/imp.html#imp.reload

Comments

0

How do you run b.py? As a side note remove *.pyc files before running script again.

1 Comment

Via Run Script button. What are pyc files? Where can I find them? I'm sorry. I just started with scripting.
0
#a.py 
num = 3 

#b.py 
import a 

print(a.num) # result is 3
a.num += 1
print(a.num) # result is 4

Next time you run b.py, result is the same.

Because a.num is always init as 3, so you maybe need to update the raw a.py file to change 3 to some other values.

But this is wired I suggest not do so.

Comments

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.