275

I have some lines of python code that I'm continuously copying/pasting into the python console. Is there a load command or something I can run? e.g. load file.py

3
  • 23
    import file. No .py extension.
    – Mikel
    Commented Mar 12, 2011 at 1:31
  • 2
    When import file is used, variables in file cannot be accessed.
    – Kadir
    Commented Jul 2, 2013 at 16:05
  • 7
    Actually, variable can be accessed. Try file.variable. Commented Jan 21, 2014 at 23:24

8 Answers 8

244

From the man page:

-i When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception.

So this should do what you want:

python -i file.py
5
  • 10
    This is a great way to go if you are not yet in the console. Thanks for the tip Arafangion Commented Nov 16, 2012 at 11:42
  • 1
    how does one reload the file.py if the file has changed? python3.8+
    – Asalle
    Commented Oct 26, 2020 at 15:43
  • 1
    The question was clearly to load the file when already in the console.
    – RichieHH
    Commented Mar 22, 2021 at 19:10
  • 1
    @RichieHH: A full decade of stack overflow visitors have found it a useful answer, however, and the original question might have been ambiguous and has since been edited...
    – Arafangion
    Commented Apr 8, 2021 at 2:47
  • 2
    @Asalle: For your question, you should put that up as a separate question, however take a look at stackoverflow.com/questions/31410419/python-reload-file
    – Arafangion
    Commented Apr 8, 2021 at 2:48
211

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
>>>
3
  • 1
    Can you explain your usage of copy con? From what I'm reading it's only used to copy files computerhope.com/copyhlp.htm Commented Mar 28, 2014 at 12:49
  • 12
    He copied from the console (treated as a file) to execfile_example.py. ^Z is end of file. It's just a way to get text into a file without opening an editor. Commented May 1, 2014 at 21:14
  • See the answer from @Arafangion if you are not yet in the console. Commented Sep 27, 2017 at 19:45
130

Python 3: new exec (execfile dropped) !

The execfile solution is valid only for Python 2. Python 3 dropped the execfile function - and promoted the exec statement to a builtin universal function. As the comment in Python 3.0's changelog and Hi-Angels comment suggest:

use

exec(open(<filename.py>).read())

instead of

execfile(<filename.py>)
5
  • if the file contains import statements e.g. import os, does exec() execute those statements? I'm trying this currently and the answer appears to be no... Commented May 21, 2020 at 17:23
  • 4
    it should be noted that filename.py is a string and so should be in quotes. i.e. exec(open("mypythonfile.py").read())
    – Max Wen
    Commented Dec 29, 2020 at 2:39
  • 4
    Well...that's a step backwards. Thanks, Python 3.
    – CrazyPyro
    Commented Feb 18, 2021 at 0:00
  • how do you supply any arguments that the program being executed needs?
    – Motorhead
    Commented Jan 22, 2022 at 19:46
  • Man that's ugly Commented Dec 6, 2022 at 18:37
36

From the shell command line:

python file.py

From the Python command line

import file

or

from file import *
5
  • 11
    And reload(file) when you've changed something in it.
    – Santa
    Commented Mar 12, 2011 at 4:34
  • 1
    That's from file import *, not import * from file
    – kindall
    Commented Mar 12, 2011 at 17:41
  • 2
    Does the file need to be a in specific director? I'm trying it with its full path and it's not working. Commented Mar 28, 2014 at 12:51
  • import directory.subdirectory.file
    – Seth
    Commented Jan 21, 2015 at 16:15
  • @Seth That's a relative path, isn't it? How about an absolute path? Commented Aug 3, 2021 at 18:01
25

You can just use an import statement:

from file import *

So, for example, if you had a file named my_script.py you'd load it like so:

from my_script import *
2
  • what does the * mean?
    – user1529540
    Commented Jun 15, 2021 at 3:31
  • 1
    The * in this context means to import all names in the script into the current scope. So if you are running this directly from the python command prompt then any variables, functions, etc. that you defined in your script would be available in your python session.
    – amicitas
    Commented Jun 16, 2021 at 13:15
6

Open command prompt in the folder in which you files to be imported are present. when you type 'python', python terminal will be opened. Now you can use

import script_name
Note: no .py extension to be used while importing.
How can I open a cmd window in a specific location?

5

If you're using IPython, you can simply run:

%load path/to/your/file.py

See http://ipython.org/ipython-doc/rel-1.1.0/interactive/tutorial.html

0

If your path environment variable contains Python (eg. C:\Python27\) you can run your py file simply from Windows command line (cmd). Howto here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.