1

I am learning Python, and I have written a script per an example in the book I am reading where it imports the urllib library. This works fine when I run it from IDLE, but if I go to the folder where the file is and run "python test.py" I get an error where it tells me that

Traceback (most recent call last):
  File "test.py", line 1, in ?
    import urllib.request
ImportError: No module named request

I verified that I am using Python 3.1.2. Any suggestions or ideas why this fails on the command line?

My code:

import urllib.request
import time

price = 99.99
while price > 1.01:
    time.sleep(3)
    page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
    text = page.read().decode("utf8")
    where = text.find('>$')
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    price = float(text[start_of_price:end_of_price])

print ("Buy!")
4
  • put the code inside a code block for readability Commented Oct 18, 2010 at 1:03
  • i thought i did, ill edit the post Commented Oct 18, 2010 at 1:06
  • can you let us know if you have both Python 2.x and 3 installed ? Commented Oct 18, 2010 at 1:07
  • i do see both 2.4 and 3 folders installed, im using a mac version 10.4 also. Commented Oct 18, 2010 at 1:08

5 Answers 5

4

urllib.request was introduced with Python 3. It is very possible that when you run the code from the command line, you are using an older, Python 2.x binary.

Type python --version on the command line to see which Python is being used.

Edit in response to Drewdin's comment

Running the Python 3.1.2 installer for Mac OS X, I see this:

NOTE: This package will by default not update your shell profile and will also not install files in /usr/local. Double-click Update Shell Profile at any time to make 3.1.2 the default Python.

The installer puts the applications in "Python 3.1" in your Applications folder, and the underlying machinery in /Library/Frameworks/Python.framework. It can optionally place links to the command-line tools in /usr/local as well, by default you have to add the "bin" directory inside the framework to you shell's search path.

So depending on how you installed it, you may already have links placed in /usr/local/bin, which will be in your path. If you chose this option at install time, all you should have to do is type python3 or python3.1 in your shell to get the updated version.

Otherwise, either double click that "Update Shell Profile", or add this to your path:

export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.1/bin

By default, Python 3.x does not make the python command alias in Unix/Linux environments because it could possibly interfere with system processes/commands dependent on the default-installed Python.

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

1 Comment

using python -v it looks like the command line is using the older version. How can i change it to use the newer version? Thanks
0

Well, the error is saying it can't find the library, so there is something different between the environments.

Here's a tutorial on python modules that could help.

Have a look at the PYTHONPATH environment variable:

 $ echo $PYTHONPATH

at least on a UNIX box. (Don't recall for Windows, honestly.)

Have a look also at Python's built in path with the line

 print sys.path

Odds on are that PYTHONPATH differs between the command line and IDLE environments.

4 Comments

Thanks, i checked your link but i did not see how to change the command line to look at the newer version. Did i just not look hard enough?
Drewdin, in your Applications folder find the Python3.1 folder (or similar) and run the .command script in there (just double click it). It will modify your PATH for you
@Parker I clicked that script and it finished stating that im all set from here on out but it didn't work. I still need to type python3.0 instead of just python. Any ideas?
What's the display when you run 'python' in your command line?
0

Either you do from urllib import request,

or you specify it during the call

import urllib
urlib.request (...)

Comments

0

Drewdin, when you type 'python' in your Terminal command line, it will display the version that it's running. If it's not 3.1, which is when urllib.request was introduced, try python3.1 test.py

Also, try import urllib instead of import urllib.request

Comments

0

I ran into this exact problem while following the same example from the same book "Head First Programming" First, I installed Python 3, as the version that ubuntu rolls with is currently Python 2:

sudo apt-get install python3

Then, you must install the corresponding Idle:

sudo apt-get install idle-python3.2

press alt+F2 to bring up your run dialogue box, type 'idle-python3.2' & hit return - you'll now see your python shell reads 'Python 3.2.2' at the top of the screen!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.