Discussion:
[python-win32] Interactive Code to Script File
DJ Webre
2014-09-16 19:13:32 UTC
Permalink
I am trying to convert a program from interactive to script file.

The program consists of the following 2 lines:

import webbrowser
webbrowser.open_new('http://www.google')

When I run it interactively, it works but if I run it as a scrip file, it produces an error unless I import webbrowser interactively.

What am I doing wrong?

Thanks in advance for any assistance.

D. J.
Bob Hood
2014-09-16 23:05:11 UTC
Permalink
Hi, DJ.
Post by DJ Webre
I am trying to convert a program from interactive to script file.
import webbrowser
webbrowser.open_new('http://www.google')
When I run it interactively, it works but if I run it as a scrip file, it
produces an error unless I import webbrowser interactively.
What am I doing wrong?
That code worked perfectly for me in a Python script file when run from the
command line (Windows 7 64-bit, Python 2.7.6 64-bit).
Vernon D. Cole
2014-09-17 00:13:52 UTC
Permalink
Dear D.J.:
First, let me mention that you violated several of the unwritten rules
of asking for help.
For example, the code you typed into for email is not the same as the code
in the picture
you sent as an attachment (using a proprietary format).
Code samples should be cut-and-paste, not retyped.
The unwritten rules have been summarized at
http://www.catb.org/~esr/faqs/smart-questions.html
<https://www.linkedin.com/redirect?url=http%3A%2F%2Fwww%2Ecatb%2Eorg%2F%7Eesr%2Ffaqs%2Fsmart-questions%2Ehtml&urlhash=VCfv&_t=tracking_disc>
This helps us help you.

Now to answer your question:

The code you typed in has an incorrect URL, but it reliably opens a browser
which informs me of that
fact when run as a script from the command line using Python versions from
2.3 to 3.4.

The only error in the code pictured in your attachment is the
over-specification of the Python version
in your #! line. It is correct to specify the version to the second level,
like
#!python2.7
but specifying the third level is an error in the Python Launcher for
Windows (which you were not using.)
As a practical matter, your code is not at all version sensitive, so you
#!/usr/bin/python
After I removed the offending ".8" that version of the program also
operated as expected in all versions tested.
I tried running it from the pythonwin IDE (as you were doing in the screen
capture) and it worked correctly
in Python 2.7 and 3.4.

This leads me to believe that the problem lies in your specific
installation of Python. In what way might your installation be unusual?
Might it, perhaps, have been installed without administrative privileges,
or for only your user (not all users)?
I am trying to convert a program from interactive to script file.
import webbrowser
webbrowser.open_new('http://www.google')
When I run it interactively, it works but if I run it as a scrip file, it
produces an error unless I import webbrowser interactively.
What am I doing wrong?
Thanks in advance for any assistance.
D. J.
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
Abhay Anant Jahagirdar
2014-09-16 22:57:48 UTC
Permalink
Hi,

You are doing it right but, for the URL string use " "(double quotes). you
should be fine
https://docs.python.org/2.0/ref/strings.html

import webbrowser
webbrowser.open_new("https://www.google.org/")


Thanks,
Abhay
Post by DJ Webre
I am trying to convert a program from interactive to script file.
import webbrowser
webbrowser.open_new('http://www.google')
When I run it interactively, it works but if I run it as a scrip file, it
produces an error unless I import webbrowser interactively.
What am I doing wrong?
Thanks in advance for any assistance.
D. J.
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
--
Thank you,
Abhay Anant Jahagirdar
MS candidate at Dept. of Computer Science
University of Southern California
Email: ***@usc.edu
Phone: (323) 578-1548
Tim Roberts
2014-09-17 17:25:00 UTC
Permalink
Post by Abhay Anant Jahagirdar
You are doing it right but, for the URL string use " "(double quotes).
you should be fine
https://docs.python.org/2.0/ref/strings.html
You do understand that there is absolutely no difference between a
single-quoted string and a double-quoted string in Python, don't you?
(Unlike PHP)
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Zachary Ware
2014-09-16 23:37:20 UTC
Permalink
On Tue, Sep 16, 2014 at 2:13 PM, DJ Webre
Post by DJ Webre
I am trying to convert a program from interactive to script file.
import webbrowser
webbrowser.open_new('http://www.google')
When I run it interactively, it works but if I run it as a scrip file, it
produces an error unless I import webbrowser interactively.
What am I doing wrong?
Thanks in advance for any assistance.
Notice the path for webbrowser in the traceback, you have a file named
'webbrowser.py' in the same folder as 'webbrowser00.py', and the
'import webbrowser' line in webrowser00 is importing your own
webbrowser module, which doesn't have 'open_new'. You'll need to
either rename 'webbroswer.py' to something else or delete it (and make
sure to delete 'webbrowser.pyc' either way).

By the way, it's usually best to just copy and paste your code and
error messages as text in the body of the email rather than attaching
an image, and embedding the image in a Word document just makes it
harder to see what's going on. Just for future reference :)
--
Zach
Nathan McCorkle
2014-09-17 00:48:31 UTC
Permalink
Post by Zachary Ware
By the way, it's usually best to just copy and paste your code and
error messages as text in the body of the email rather than attaching
an image, and embedding the image in a Word document just makes it
harder to see what's going on. Just for future reference :)
Huh, I didn't even see the attachment the first or second time I looked at
this email thread.

There is also pastebin and similar short-or-long-lived code snippet pasting
sites that even sometimes handle syntax highlighting and such.
Zachary Ware
2014-09-17 04:48:17 UTC
Permalink
That was it. Thanks all for the assistance. I will also incorporate the
suggestions in future request for help. Thanks again.
Glad I could be of help :).

Regards,
--
Zach
Loading...