Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a possibility to execute a Python script while being in interactive mode

Normally you can execute a Python script for example: python myscript.py, but if you are in the interactive mode, how is it possible to execute a Python script on the filesystem?

>>> exec(File) ??? 

It should be possible to execute the script more than one time.

like image 270
lennykey Avatar asked Jan 07 '11 10:01

lennykey


People also ask

How do I run a Python script in interactive mode?

The interactive Python mode lets you run your script instantly via the command line without using any code editor or IDE. To run a Python script interactively, open up your command line and type python. Then hit Enter. You can then go ahead and write any Python code within the interactive mode.

Can we do Python programming in interactive mode?

There are two modes through which we can create and run Python scripts: interactive mode and script mode. The interactive mode involves running your codes directly on the Python shell which can be accessed from the terminal of the operating system. In the script mode, you have to create a file, give it a name with a .

When Python is running in the interactive mode?

The correct answer to the question “When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you” is option (a). What Python script would you like me to run?

How do I run a Python script from IDLE interactive shell?

To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.


2 Answers

Use execfile('script.py') but it only work on python 2.x, if you are using 3.0 try this

like image 179
fn. Avatar answered Oct 04 '22 00:10

fn.


import file without the .py extension will do it, however __name__ will not be "__main__" so if the script does any checks to see if it's being run interactively you'll need to bypass them.

Alternately, if you're wanting to have a look at the environment after the script runs try python -i script.py

EDIT: To load it again

file = reload(file)

like image 41
richo Avatar answered Oct 04 '22 01:10

richo