当我运行两次python脚本时,文件名中出现语法错误?
在IPython中尝试两次使用当我运行两次python脚本时,文件名中出现语法错误?,python,windows,python-2.7,ipython,Python,Windows,Python 2.7,Ipython,在IPython中尝试两次使用run命令时,第二次出现语法错误: In [2]: run control.py In [3]: run control.py File "<ipython-input-3-3e54b0f85f39>", line 1 run control.py ^ SyntaxError: invalid syntax [2]中的:运行control.py 在[3]中:运行control.py 文件“”,第1行 运行con
run
命令时,第二次出现语法错误:
In [2]: run control.py
In [3]: run control.py
File "<ipython-input-3-3e54b0f85f39>", line 1
run control.py
^
SyntaxError: invalid syntax
[2]中的:运行control.py
在[3]中:运行control.py
文件“”,第1行
运行control.py
^
SyntaxError:无效语法
在我的脚本中,我实现了一个类,并从该类生成了一个对象
我尝试在没有任何类和对象的情况下创建一个新的python脚本,当我多次运行它时,它工作得很好,为什么类和对象会产生问题
我在Windows(而不是笔记本电脑)上使用ipython。正确的ipython命令是
%run
,前缀为%
。没有前缀也可以工作,但前提是您没有Python全局名称run
屏蔽命令
由于%run script.py
在IPython解释器内运行脚本,因此脚本完成后,解释器中可以使用脚本设置的所有全局变量。您的console.py
脚本设置全局名称run
,该名称现在屏蔽了%run
命令
换句话说,这与类或实例无关。使用%run
运行脚本,或者不要在脚本本身的任何位置使用名称run
演示使用一个小脚本设置运行;请注意%run
如何继续工作,以及删除名称run
如何使run demo.py
再次工作:
In [1]: %cat demo.py
print('Setting the name "run" to "foo"')
run = 'foo'
In [2]: run demo.py
Setting the name "run" to "foo"
In [3]: run # the global name is set
Out[3]: 'foo'
In [4]: run demo.py
File "<ipython-input-4-3c6930c3028c>", line 1
run demo.py
^
SyntaxError: invalid syntax
In [5]: %run demo.py
Setting the name "run" to "foo"
In [6]: del run # deleting the global name makes 'run script' work again
In [7]: run demo.py
Setting the name "run" to "foo"
[1]中的:%cat demo.py
打印('将名称“run”设置为“foo')
run='foo'
在[2]中:运行demo.py
将名称“run”设置为“foo”
在[3]:run#中设置全局名称
Out[3]:“foo”
在[4]中:运行demo.py
文件“”,第1行
运行demo.py
^
SyntaxError:无效语法
在[5]中:%run demo.py
将名称“run”设置为“foo”
在[6]:del run#删除全局名称会使“run script”再次工作
在[7]中:运行demo.py
将名称“run”设置为“foo”