Discussion:
Import Doesn't Import
ryguy7272
2014-10-16 00:40:25 UTC
Permalink
So sorry everyone. I've posted here several times today. This is VERY frustrating.

So, I'm reading this link.
https://docs.python.org/2/howto/urllib2.html


Fetching URLs
The simplest way to use urllib2 is as follows:
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()


So, I fire up Python, and create a new file and name it and hit F5.

All I have is thins in the file:
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()

It immediately produces this error:
Traceback (most recent call last):
File "C:/Python34/import_web_data.py", line 1, in <module>
import urllib2
ImportError: No module named 'urllib2'


ImportError: No module named 'urllib2'
I'm telling Python to import because it doesn't exist and it throws an error. I don't get it; I just don't get it. If I'm working with R, I can import thousands of libraries with no errors whatsoever. With Python, I just get thousands of errors with nothing working whatsoever. I totally don't understand this language. Import means import. Right. WTF!!!!!
ryguy7272
2014-10-16 00:44:00 UTC
Permalink
This post might be inappropriate. Click to display it.
Dan Stromberg
2014-10-16 01:19:37 UTC
Permalink
Post by ryguy7272
Either this is the most brilliant thing ever invented, or it's the biggest piece of shit ever invented. I just can't tell. All I know for sure, is that it doesn't do ANYTHING that I tell it to do.
Maybe you should decide whether you want to run your 2.x code on 2.7,
or adapt it to 3.x. Doing both won't work well, unless you go with a
version of the code carefully constructed to work on both.

I believe I heard there's a way of doing Python in Visual Studio, but
personally, I use Python on Linux without an IDE - mostly via a vim
macro.

BTW, I got your code to run, but it got stuck in a socket.recv(); I
suspect Wikipedia is blocking some forms of access, so this labor may
be irrelevant.

Perhaps check out
https://pypi.python.org/pypi?%3Aaction=search&term=stock&submit=search
, and use the most relevant-looking one with a matching version of
Python?
Terry Reedy
2014-10-16 02:43:10 UTC
Permalink
Perhaps you are trying too hard too fast. When I started Python, after
20 years of on and off experience with multiple languages, I went
through the tutorial (on Dos, no Idle). It took about 2 hours. Then I
quickly wrote the code I needed for a paid project.

Do stick with 3.4.2, but use the 3.4.2 manual.
Post by ryguy7272
Either this is the most brilliant thing ever invented,
This, I think.
Post by ryguy7272
or it's the biggest piece of shit ever invented.
Definitely not. It is possible that Python will never fit your brain.
But give it a bit more of a chance.
Post by ryguy7272
All I know for sure, is that it doesn't do ANYTHING
that I tell it to do.
Because you have told it to do things like run a program with syntax
error, or import something that does not exist. I don't know of any
language than does that ;-).
--
Terry Jan Reedy
Mark Lawrence
2014-10-16 08:01:49 UTC
Permalink
On 16/10/2014 01:44, ryguy7272 wrote:

If you carry on using this approach and you continue using the buggy
google groups interface you're unlikely to make many friends. May I
suggest that before posting you spend a few minutes doing some research
as Python has been in use for 22 years so your questions have been
answered many times before.
Post by ryguy7272
Either this is the most brilliant thing ever invented, or it's the biggest piece of shit ever invented. I just can't tell. All I know for sure, is that it doesn't do ANYTHING that I tell it to do.
A bad workman always blames his tools.
Post by ryguy7272
If there is another way of running Python code, like using Visual Studio, or a text file, or some such thing, let me know, and I'll do it. It seems like you should use Python to run Python, but my Python doesn't do anything at all. Yesterday I uninstalled Python 2.7, because that wouldn't do anything either. I'm just about ready to uninstall 3.4.
https://docs.python.org/3/using/windows.html
https://docs.python.org/3/using/windows.html#launcher
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence
Chris Angelico
2014-10-16 00:54:01 UTC
Permalink
Post by ryguy7272
I totally don't understand this language. Import means import. Right. WTF!!!!!
Yes, but import from where? If you ask Python - or any other language
- to "import asfadgt4tfihavzcxvzxcvaerg", do you expect it to succeed?
It doesn't exist.

In this case, you simply want "urllib", not "urllib2". You should be
able to "import urllib" with no problems.

ChrisA
Dan Stromberg
2014-10-16 01:12:15 UTC
Permalink
Post by ryguy7272
ImportError: No module named 'urllib2'
http://stackoverflow.com/questions/2792650/python3-error-import-error-no-module-name-urllib
MRAB
2014-10-16 01:13:43 UTC
Permalink
Post by ryguy7272
So sorry everyone. I've posted here several times today. This is VERY frustrating.
So, I'm reading this link.
https://docs.python.org/2/howto/urllib2.html
Fetching URLs
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
So, I fire up Python, and create a new file and name it and hit F5.
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
File "C:/Python34/import_web_data.py", line 1, in <module>
import urllib2
ImportError: No module named 'urllib2'
ImportError: No module named 'urllib2'
I'm telling Python to import because it doesn't exist and it throws
an error. I don't get it; I just don't get it. If I'm working with
R, I can import thousands of libraries with no errors whatsoever.
With Python, I just get thousands of errors with nothing working
whatsoever. I totally don't understand this language. Import means
import. Right. WTF!!!!!
It raises an exception because there's no such module as 'urllib2' in
Python 3.

You're reading the docs here:

https://docs.python.org/2/howto/urllib2.html

which are for Python 2, but the path you have here:

C:/Python34/import_web_data.py

says that you're using Python 3.4.

There's an explanation of Python 2 vs Python 3 here:

https://wiki.python.org/moin/Python2orPython3
Ben Finney
2014-10-16 01:35:29 UTC
Permalink
Post by ryguy7272
So, I'm reading this link.
https://docs.python.org/2/howto/urllib2.html
Note that this is the documentation for Python 2, which is obsolescent.
It has had a long life, so references to Python on the web are still
dominantly about that legacy version. Your confusion is quite normal.

Instead, use Python 3 only (which it seems you are doing), and read the
Python 3 documentation. You'll find that the ?urllib? modules
<URL:https://docs.python.org/3/library/urllib.html> are what you want.
Post by ryguy7272
I don't get it; I just don't get it.
It's unfortunate that a lot of new Python users will be confused by this
long transition. But the situation is a lot better now; more and more
Python articles on the web are referring to Python 3 which is the
actively-developed version, recommended for new users.
--
\ ?You don't need a book of any description to help you have some |
`\ kind of moral awareness.? ?Dr. Francesca Stavrakoloulou, bible |
_o__) scholar, 2011-05-08 |
Ben Finney
Chris Rebert
2014-10-16 01:21:25 UTC
Permalink
Post by ryguy7272
So sorry everyone. I've posted here several times today. This is VERY frustrating.
So, I'm reading this link.
https://docs.python.org/2/howto/urllib2.html
Important note!: The "/2/" in the URL means those docs are for Python 2.x
When using Python 3, ensure that the docs you're consulting have a "/3/" in
them instead.
Post by ryguy7272
Fetching URLs
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
So, I fire up Python, and create a new file and name it and hit F5.
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
File "C:/Python34/import_web_data.py", line 1, in <module>
import urllib2
ImportError: No module named 'urllib2'
You're using Python 3, and the urllib2 module no longer exists in Python 3.
The URL/HTTP modules got refactored on Python 3. You want the
`urllib.request` module instead.

Although most folks nowadays use http://docs.python-requests.org/ instead,
though it's third-party and outside the std lib.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141015/601603ce/attachment.html>
Emile van Sebille
2014-10-16 21:29:14 UTC
Permalink
Post by ryguy7272
ImportError: No module named 'urllib2'
I'm telling Python to import because it doesn't exist and it throws an error. I don't get it; I just don't get it. If I'm working with R, I can import thousands of libraries with no errors whatsoever. With Python, I just get thousands of errors with nothing working whatsoever. I totally don't understand this language. Import means import. Right. WTF!!!!!
So, maybe Import doesn't mean import -- perhaps you expect import to
retrieve content from the web and make it importable(1). That's not what
python's import does. Python's version looks for a local module to import.

I'd suggest starting with the tutorial.

Emile


(1) which would imply execution of arbitrary code.
Joel Goldstick
2014-10-16 22:28:26 UTC
Permalink
Post by Emile van Sebille
Post by ryguy7272
ImportError: No module named 'urllib2'
I'm telling Python to import because it doesn't exist and it throws an
error. I don't get it; I just don't get it. If I'm working with R, I can
import thousands of libraries with no errors whatsoever. With Python, I
just get thousands of errors with nothing working whatsoever. I totally
don't understand this language. Import means import. Right. WTF!!!!!
So, maybe Import doesn't mean import -- perhaps you expect import to
retrieve content from the web and make it importable(1). That's not what
python's import does. Python's version looks for a local module to import.
I'd suggest starting with the tutorial.
Emile
(1) which would imply execution of arbitrary code.
--
https://mail.python.org/mailman/listinfo/python-list
requests is a better choice than urllib
--
Joel Goldstick
http://joelgoldstick.com
Mark Lawrence
2014-10-16 22:56:03 UTC
Permalink
Post by Joel Goldstick
Post by Emile van Sebille
Post by ryguy7272
ImportError: No module named 'urllib2'
I'm telling Python to import because it doesn't exist and it throws an
error. I don't get it; I just don't get it. If I'm working with R, I can
import thousands of libraries with no errors whatsoever. With Python, I
just get thousands of errors with nothing working whatsoever. I totally
don't understand this language. Import means import. Right. WTF!!!!!
So, maybe Import doesn't mean import -- perhaps you expect import to
retrieve content from the web and make it importable(1). That's not what
python's import does. Python's version looks for a local module to import.
I'd suggest starting with the tutorial.
Emile
(1) which would imply execution of arbitrary code.
--
https://mail.python.org/mailman/listinfo/python-list
requests is a better choice than urllib
To an experienced user yes, to an inexperienced OP who can't even run a
Python program without help a very emphatic no.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence
Continue reading on narkive:
Loading...