Reputation: 11
I am trying to run this following script in here and getting this NameError. I have added path variable in Windows 7.
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
This is the script I am trying to run :
#!/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 ".",
Thank you all.
Upvotes: 1
Views: 179
Reputation: 16993
You need to do python script.py
instead, from the command prompt not from the Python interpreter.
However, if you are in the Python interactive interpretor in the same directory as your script, as @Tuan333 points out, you can do:
>>> import script
or
>>> from script import something
to get access to functions and classes defined in the script (note that it is script
in this case, not script.py
because you are treating it as a module). However, as @ŁukaszRogalski points out, this is not equivalent to running the script from the command prompt as described above.
Upvotes: 4
Reputation: 340
you have to run your script by using this command in command prompt
python [Address of your script]
Upvotes: 1
Reputation: 37539
You need to call the script all on one line
C:\Users\myname>python C:\path\to\script.py
Upvotes: 1