Code run from IDLE but not via double-clicking on its *.py

N

n00m

When I double-click on "some.py" file console window appears just for a
moment and right after that it's closed. If this script is started from
inside of IDLE (F5 key) then it executes as it should be (e.g.
executing all its print statements).

Any ideas? OS: Windows; Python 2.3.4. Thanks.
 
M

max

When I double-click on "some.py" file console window appears just
for a moment and right after that it's closed. If this script is
started from inside of IDLE (F5 key) then it executes as it
should be (e.g. executing all its print statements).

Any ideas? OS: Windows; Python 2.3.4. Thanks.

It's a windows thing. The shell windows opens to run the script in is
closing when the script finishes.

Try opening a shell (Start->Run: cmd.exe on NT/2k/XP or Start->Run:
command.com on 95/98) and then either type the full path to the script
or navigate to it and execute it.

max
 
M

mensanator

n00m said:
When I double-click on "some.py" file console window appears just for a
moment and right after that it's closed. If this script is started from
inside of IDLE (F5 key) then it executes as it should be (e.g.
executing all its print statements).

Any ideas? OS: Windows; Python 2.3.4. Thanks.

Your console window closes when the program terminates.
And no, you won't necessarily see any of your print statements
before the window closed. They were buffered and the window
closed before they could be displayed.

Start a console window manually. From the command prompt,
type

some.py

or

python some.py
 
N

n00m

Oops.. not everything so super as I thought.
Incredible but from command line it results as:

D:\>python23\python d:\python23\00\socket6.py
Traceback (most recent call last):
File "d:\python23\00\socket6.py", line 1, in ?
import socket, thread
File "D:\Python23\00\socket.py", line 3, in ?
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_INET'

D:\>

FROM IDLE 1.0.3 THE SAME SCRIPT WORKS PERFECTLY !!!

WHY ON THE EARTH <'module' object has no attribute 'AF_INET'> ???
 
R

Richie Hindle

[n00m]
WHY ON THE EARTH <'module' object has no attribute 'AF_INET'> ???

Because you have a socket.py in d:\python23\00 which is being picked up
instead of Python's own socket module. You shouldn't give your modules
the same name as Python's own modules.
 
N

n00m

Richie said:
Because you have a socket.py in d:\python23\00 which is being picked up
instead of Python's own socket module. You shouldn't give your modules
the same name as Python's own modules.

Yes, Richie! YOU are dmndly RIGHT! Thanks.
 
N

n00m

Funnily but I still can't get the code working... WITHOUT IDLE.
I think it's because of "import thread" line. Seems something
wrong with "opening" this module. In IDLE it works OK.
 
R

Richie Hindle

[n00m]
Funnily but I still can't get the code working... WITHOUT IDLE.
I think it's because of "import thread" line. Seems something
wrong with "opening" this module. In IDLE it works OK.

It's difficult to diagnose your problem with so little information. Please
post:

o The command you're typing into the command prompt
o The error message you're getting
o The full traceback
o The code you're trying to run, or if it's too big then the piece that
the last line of the traceback refers to

Thanks,
 
S

Steve Holden

n00m said:
Funnily but I still can't get the code working... WITHOUT IDLE.
I think it's because of "import thread" line. Seems something
wrong with "opening" this module. In IDLE it works OK.

Now, let's see ... [presses fingers to temples and exercises psychic
powers] ... ah yes, its because you're DOING SOMETHING WRONG :)

Unfortunately "can't get the code working" isn't a very helpful
description. Can you explain (preferably with a traceback) how it fails?

Generally speaking, when you take your car into the garage (American:
shop) because "it won't go" it's OK because the technicians can try and
start it themselves. We don;t have your code readily to hand, so we need
a bit more to go on.

Generally speaking you would do well to try the threading module rather
than thread unless you have a specific reason for not doing so. But if
you post your code (or even better a subset of your code that
demonstrates the error so people can pick it apart for themselves) with
a traceback from the interpreter or a full description of what you
expected the code to do and what it actually does we'll be able to help
much more easily.

regards
Steve
 
N

n00m

Richie; Steve; Thanks for your replies!
o The command you're typing into the command prompt
o The error message you're getting
o The full traceback
o The code you're trying to run, or if it's too big then the piece that
the last line of the traceback refers to

1.
D:\>python23\python d:\python23\socket6.py [Enter]

It's OK so far. Python code is launched and starts listening
to port 1434 (see the code below; it's the same code as in my
neibouring topic).
Now I launch a vbs script (which will connect to port 1434).
I.e. I just double-click "my.vbs" file.
And... voila! In a moment & silently console window closes
without any error messages (or I just don't see them).
But VBS reports a network error. Tested on win2k and win98.


In IDLE it works ABSOLUTELY FINE! Prints statements in the
code do exactly what they must do.


import socket, thread
host, port = '127.0.0.1', 1434
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((host, 1433))
s1.bind((host, port))
s1.listen(1)
cn, addr = s1.accept()

def VB_SCRIPT():
while 1:
data = cn.recv(4096)
if not data: return
s2.send(data)
print 'VB_SCRIPT:' + data + '\n\n'

def SQL_SERVER():
while 1:
data = s2.recv(4096)
if not data: return
cn.send(data)
print 'SQL_SERVER:' + data + '\n\n'

thread.start_new_thread(VB_SCRIPT,())
thread.start_new_thread(SQL_SERVER,())
 
N

n00m

Steve said:
Now, let's see ... [presses fingers to temples and exercises psychic
powers] ... ah yes, its because you're DOING SOMETHING WRONG :)

I just admire this sort of humour!
Made me chuckling and (even) laughing.
 
D

Dennis Lee Bieber

1.
D:\>python23\python d:\python23\socket6.py [Enter]

It's OK so far. Python code is launched and starts listening
to port 1434 (see the code below; it's the same code as in my
neibouring topic).

It is not clear if this is being typed at a command prompt in a
console window (start/programs/accessories/command prompt) or the run
prompt (start/run... )

Consoles opened via start/run (or double clicking on a .py) always
close when the program exits. A command shell, opened all by itself,
into which one has typed commands, should not close regardless of what
happens inside it.
import socket, thread
host, port = '127.0.0.1', 1434
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((host, 1433))

So who are you connecting to? I may have missed it, but who is
listening on 1433?
--
 
R

Richie Hindle

[n00m]
D:\>python23\python d:\python23\socket6.py [Enter]

It's OK so far. Python code is launched and starts listening
to port 1434 (see the code below; it's the same code as in my
neibouring topic).
Now I launch a vbs script (which will connect to port 1434).
I.e. I just double-click "my.vbs" file.
And... voila! In a moment & silently console window closes
without any error messages (or I just don't see them).
But VBS reports a network error. Tested on win2k and win98.

That sounds impossible, so I must be misunderstanding something. What
happens when you do this (forgive me if this seems patronising, but I'm
missing something about the way you're working)

1. Start a new Command Prompt via Start / Programs / Accessories / Command
Prompt (or the equivalent on your machine)

2. Type the following: d:\python23\python d:\python23\socket6.py [Enter]

3. Double-click your .vbs file in Windows Explorer.

Now what does the python Command Prompt say? By your description above,
it sounds like it disappears, but that ought to be impossible.
 
N

n00m

It's soooooo pity I'm too buzy at my work today.
I'll reply a bit later. Thank you, guys!

PS Port 1433 SQL Server listens to.
PPS SQL Server is a rdbms from M$.
 
D

Dennis Lee Bieber

PS Port 1433 SQL Server listens to.

Okay... Whilst I have a copy of MSDE on this machine, I've not
configured it -- and never had to worry about the IP numbers on any
database, the dp-api adapter defaults took care of that for me.
(Actually, I've go overkill on this machine -- MSDE, MaxDB (SAP via
MySQL), MySQL, Firebird, and the everpresent JET.
PPS SQL Server is a rdbms from M$.

That, at least, I recognized (as I recall, it started life as a
licensed version of Sybase which has diverged over the years)
--
 
N

n00m

Dennis; Richie;
That sounds impossible, so I must be misunderstanding something.
YOU - BOTH - UNDERSTAND ME ABSOLUTELY RIGHT!
1.
Start a new Command Prompt via Start / Programs / Accessories / Command
Prompt (or the equivalent on your machine)
2.
Type the following: d:\python23\python d:\python23\socket6.py [Enter]
3. Double-click your .vbs file in Windows Explorer.
Now what does the python Command Prompt say?

It says... NOTHING! It just DISAPPEARS!

JUST FOR TO MAKE SURE I put in the same directory (Python's root)
testme.py file with this simplest content:

print 'ONE'
print 'TWO'
print 'THREE'

Then I run it EXACTLY the same way as I run socket6.py:

D:\>python23\python d:\python23\testme.py [Enter]
ONE
TWO
THREE

D:\>

AND OF COURSE CONSOLE WINDOW DID NOT CLOSE AFTER THAT !
BUT IT REALLY DISAPPEARS IN CASE OF socket6.py !
(but only AFTER I double-click my .vbs file in Windows Explorer)
 
D

Dennis Lee Bieber

AND OF COURSE CONSOLE WINDOW DID NOT CLOSE AFTER THAT !
BUT IT REALLY DISAPPEARS IN CASE OF socket6.py !
(but only AFTER I double-click my .vbs file in Windows Explorer)

I'd be tempted to blame the VBS script then... Almost sounds like it
not only does whatever you think it does, but then chases down every
process it was talking to, and killing them too (which means getting the
console window and killing it).

What happens if you run your odd Python program in one window, and
then run /another/ Python program in another window that pretends to be
the VBS process, connecting to the socket, and sending something of the
form needed to be relayed to the SQL Server process.
--
 
D

Dennis Lee Bieber

On 1 Sep 2005 09:36:48 -0700, "n00m" <[email protected]> declaimed the
following in comp.lang.python:

<something completely snipped>

I'm going to go back a few messages... Looking for a
simplification...



=====> ORIGINAL CODE
import socket, thread
host, port = '127.0.0.1', 1434
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((host, 1433))
s1.bind((host, port))
s1.listen(1)
cn, addr = s1.accept()

def VB_SCRIPT():
while 1:
data = cn.recv(4096)
if not data: return
s2.send(data)
print 'VB_SCRIPT:' + data + '\n\n'

def SQL_SERVER():
while 1:
data = s2.recv(4096)
if not data: return
cn.send(data)
print 'SQL_SERVER:' + data + '\n\n'

thread.start_new_thread(VB_SCRIPT,())
thread.start_new_thread(SQL_SERVER,())

TWO threads, both just infinite loops of the same nature (you could
actually have done ONE def and passed different arguments in to
differentiate the two thread invocations.

However, threads aren't really needed for this simple connection
relay... The following has /not/ been run (since I don't have your
server nor VBS) but should do about the same thing (see comments for one
lack).

=======> Suggested code
import socket
import select

host = "127.0.0.1"
DBMSPort = 1433
MyPort = 1434

MySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
DBMSSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

DBMSSocket.connect((host, DBMSPort))

MySocket.bind((host, MyPort))
MySocket.listen(1)

Client, Addr = MySocket.accept()

while True:
# wait for either connection to have readable data
(inbound, outbound, excption) = select.select([DBMSSocket, Client],
[], [])
# handle each readable socket
# NOTE: there is no way (in this quick and dirty code) to
# detect end of connections.
# probably need to do things with the excption list --
# passing in the sockets, and closing them when they
# show up in excption -- actually, if one side closes
# there is no reason to continue processing the other
# side, so on any excption, could close both and exit
for s in inbound:
data = s.recv(4096)
if s is Client:
print "From VBS: ",
MyDBMS.send(data)
elif s is MyDBMS:
print "From DBMS: ",
Client.send(data)
else:
print "\n\n*** ERROR: select() socket is not known!\n\n"
print repr(data)
print "\n"

--
 
R

Richie Hindle

[Richie]
Now what does the python Command Prompt say?
[n00m]
It says... NOTHING! It just DISAPPEARS!

That's the strangest thing I've heard all week. :cool:

OK, one more thing to try - redirect the output of Python to some files
and see whether anything useful appears in the files:

C:\> d:
D:\> cd \python23
D:\> python d:\python23\socket6.py > out.txt 2> err.txt

Does anything appear in d:\python23\out.txt or d:\python23\err.txt?

[Dennis]
I'd be tempted to blame the VBS script then...

n00m, can you post the vbs?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top