问题:如何将文件加载到python控制台中?

我有一些python代码行,它们正在不断地复制/粘贴到python控制台中。有load命令或我可以运行的东西吗?例如load file.py

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


回答 0

对于Python 2(请参阅Python 3的其他答案),请尝试一下:

execfile('file.py')

用法示例:

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

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
>>>

For Python 2 (see other answers for Python 3) give this a try:

execfile('file.py')

Example usage:

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

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
>>>

回答 1

从手册页:

-i当脚本作为第一个参数传递或使用-c选项时,请在执行脚本或命令后进入交互模式。它不会读取$ PYTHONSTARTUP文件。当脚本引发异常时,这对于检查全局变量或堆栈跟踪很有用。

所以这应该做你想要的:

python -i file.py

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

回答 2

Python 3:新的exec (已删除execfile)

execfile解决方案仅对Python 2有效。Python3删除了execfile函数-并将exec语句提升为内置的通用函数。正如Python 3.0的changelog中的注释和Hi-Angels注释所建议的那样:

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

代替

execfile(<filename.py>)

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>)

回答 3

从shell命令行:

python file.py

从Python命令行

import file

要么

from file import *

From the shell command line:

python file.py

From the Python command line

import file

or

from file import *

回答 4

您可以只使用import语句:

from file import *

因此,例如,如果您有一个名为的文件,则将其my_script.py加载为:

from my_script import *

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 *

回答 5

如果您使用的是IPython,则可以简单地运行:

%load path/to/your/file.py

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

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


回答 6

存在要导入文件的文件夹中的打开命令提示符。当您键入“ python”时,将打开python终端。现在您可以使用

导入脚本名称
注意:导入时不使用.py扩展名。
如何在特定位置打开cmd窗口?

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?

回答 7

如果您的path环境变量包含Python(例如C:\Python27\),则可以直接从Windows命令行(cmd)运行py文件。 如何在这里。

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。