1

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.

2
  • You are trying to type filename in interactive python shell (after launching python). Try python script.py in Windows' cmd. Commented Sep 19, 2016 at 18:24
  • You have a small typo in your code. You might remove the , at the end after the print statement. Commented Sep 19, 2016 at 18:25

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

your solution would indeed be ideal. I just want to elaborate a bit more: if you have a script, you can also run it using interactive python interpreter by doing >>> import script
@Tuan333 import script, not script.py, and this approach is not equivalent to running python script.py (in general case).
@ŁukaszRogalski ah yes. there should not be .py in there. You are right, doesn't work in general, but in some simple case as this example, it should suffice.
1

You need to call the script all on one line

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

Comments

1

you have to run your script by using this command in command prompt
python [Address of your script]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.