How to Execute a File Within the Python Interpreter

How to execute a file within the Python interpreter?

Several ways.

  • From the shell

    python someFile.py
  • From inside IDLE, hit F5.

  • If you're typing interactively, try this (Python3):

    >>> exec(open("filename.py").read())
  • For Python 2:

    >>> variables= {}
    >>> execfile( "someFile.py", variables )
    >>> print variables # globals from the someFile module

How can I use a specific python interpreter to run my python file

You can add the path to the virtual environment's interpreter directly to the shebang at the top of the script. For example, if your virtual environment is stored at /home/ishant/venv, the shebang would be

#!/home/ishant/venv/bin/python

Then, if you execute your script directly (after making it executable with chmod +x or the like), your virtual environment will be used.

(Activating a virtual environment simply updates your PATH variable so that python resolves to the virtual environment, rather than your "regular" environment. You can always access the tools in the virtual enivironment directly instead.)

running python script in interactive python prompt and keep the variables?

Try the following for Python 2.x:

>>> execfile('first.py')

For Python 3.x, try this:

>>> exec(open("./first.py").read())

The variables should then be available to you.

Execute a file with arguments in Python shell

execfile runs a Python file, but by loading it, not as a script. You can only pass in variable bindings, not arguments.

If you want to run a program from within Python, use subprocess.call. E.g.

import subprocess
subprocess.call(['./abc.py', arg1, arg2])

How do I load a file into the python console?

For Python 2 give execfile a try. (See other answers for Python 3)

execfile('file.py')

Example usage:

Let's use "copy con" to quickly create a small script file...

C:\junk>copy con execfile_example.py
a = [9, 42, 888]
b = len(a)
^Z
1 file(s) copied.

...and then let's load this script like so:

C:\junk>\python27\python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile('execfile_example.py')
>>> a
[9, 42, 888]
>>> b
3
>>>

run program in Python shell

Use execfile for Python 2:

>>> execfile('C:\\test.py')

Use exec for Python 3

>>> exec(open("C:\\test.py").read())

How to execute Python code from within Visual Studio Code

Here is how to configure Task Runner in Visual Studio Code to run a .py file.

In your console, press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Apple). This brings up a search box where you search for "Configure Task Runner"

Sample Image

If this is the first time you open the "Task: Configure Task Runner", you need to select "other" at the bottom of the next selection list.

This will bring up the properties which you can then change to suit your preference. In this case you want to change the following properties;

  1. Change the Command property from "tsc" (TypeScript) to "Python"
  2. Change showOutput from "silent" to "Always"
  3. Change args (Arguments) from ["Helloworld.ts"] to ["${file}"] (filename)
  4. Delete the last property problemMatcher
  5. Save the changes made

Sample Image

You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B (Windows) or Cmd + Shift + B (Apple).



Related Topics



Leave a reply



Submit