2016-09-19 104 views
1

我想在这里运行以下脚本并获取此NameError。我已经加入PATH变量在Windows 7Windows 7中的Python脚本错误

C:\Users\myname>python 
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 

>>> script.py 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'script' is not defined 

这是我试图运行脚本:

#!/usr/bin/python 
import sys, string, time 
from socket import * 
host = "192.168.0.98" 
port = 80 
print "Open TCP connections to: %s, port %s" % (host, port) 
while (1): 
    s = socket(AF_INET,SOCK_STREAM) 
    s.connect((host, port)) 
    s.send("abc")  
### test: donot close. s.close() 
time.sleep(0.1) 
print ".", 

谢谢大家。

+0

您试图运行脚本在交互式python shell中输入文件名(启动'python'后)。在Windows的'cmd'中试试'python script.py'。 –

+0

您的代码中有一个小错字。在打印语句之后,您可以在末尾删除','。 – albert

回答

4

你需要做python script.py而不是从Python解释器的命令提示符而不是

但是,如果你是在同一目录下的脚本Python交互式解释器,为@ Tuan333指出,你可以这样做:

>>> import script 

>>> from script import something 

到可以访问脚本中定义的函数和类(请注意,在这种情况下为script,而不是script.py,因为您将它视为模块)。但是,如@ŁukaszRogalski指出,这不等同于如上所述从命令提示符运行脚本。

+2

你的解决方案确实是理想的。我只想详细说明一点:如果你有一个脚本,你也可以通过执行'>>> import script'使用交互式Python解释器来运行它。 – TuanDT

+0

@ Tuan333好点! – elethan

+1

@ Tuan333'导入脚本',而不是'script.py',这种方法**不等同于运行'python script.py'(一般情况下)。 –

1

你需要调用的脚本都在同一行

C:\Users\myname>python C:\path\to\script.py 
1

你必须在命令提示符下使用此命令
巨蟒[脚本的地址]