Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will changes made to a Python script affect another run in progress on the same file?

Tags:

python

Suppose that I have put a Python script to run. Let's say while it is running, I open the source code and change the value of a variable to different value. Now, in another terminal if I start running the latest source code, what happens to the previous run that is progress?

Will it be get affected because of this latest change that I did while I was running it?

The thing is that I want to do parallel runs of the program for different values of a particular variable. Any better way to do this?

like image 453
London guy Avatar asked Jun 26 '15 16:06

London guy


2 Answers

Python compiles your source into bytecode and runs that bytecode. Once the source file has been read it is no longer needed to run that bytecode. Changes to the source file won't then affect already running code.

However, if an exception is raised and Python tries to format a traceback for display, it'll reach back to the source code, mapping line markers in the bytecode back to source lines. If the source file changed after compilation, this could mean the wrong lines are being displayed. That can create confusion.

You can easily give your program command line parameters to vary how it behaves. Take a look at the sys.argv list, and perhaps the argparse module for more complex command-line option handling. That way your code remains stable and flexible.

like image 155
Martijn Pieters Avatar answered Oct 21 '22 04:10

Martijn Pieters


Python typically compiles the source code to a *.pyc file. Changing the value in the script usually won't affect the value already in memory.

The better way to do this is take an argument from argv

python your_script.py value

You can access it with

import sys
sys.argv[1] #this is the 'value' from the command line
like image 28
Leland Barton Avatar answered Oct 21 '22 06:10

Leland Barton