How do I load a file into the python console?

前端 未结 8 2218
攒了一身酷
攒了一身酷 2020-12-12 09:28

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

相关标签:
8条回答
  • 2020-12-12 10:01

    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 讨论(0)
  • 2020-12-12 10:05

    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>)
    
    0 讨论(0)
提交回复
热议问题