Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Welby Obeng
20,340 Pointsimporting question in python
How come I can do :
import datetime and get datetime.datetime.now()
I know datetime is the library, next datetime is class, and the now() is method
but I can't do :
import urllib and get urllib.request.urlopen(link).
shouldnt urllib be library, request be a class and urlopen() be the method like the above?
4 Answers
Gavin Ralston
28,770 PointsI'm not sure the code is formatted in your post just right. I think you're saying you can do the following:
import datetime
print(datetime.datetime.now()) # or whatever you want to do with it
but you're having trouble with doing this
import urllib
dosomething(urllib.request.urlopen('http://teamtreehouse.com')) # or whatever you wanted to do here
Which.... you should be able to do. urllib.request.urlopen(<link>) should provide you with an http response object, which you can then use to extract whatever information you want, like
page = urllib.request.urlopen('http://whatever.com')
page.readall()
#or
page.getcode() # to get the server response
Matthew Proudman
11,879 PointsImporting is simply telling python interpreter 'i what to work with Time'
but u need to describe what you want to do in the context of your code
import time # which will is the whole module
from time import sleep # this will import a class from a module so you don't need to address e.g. time.sleep(5)
it should be noted that under pep8 each module call (import time) should each have their own line.
Gavin Ralston
28,770 PointsWith datetime I could see where preserving the namespace requirement could be handy, what with date and time being names you might have already used somewhere else.
Kenneth Love
Treehouse Guest TeacherUh, I just did
import urllib
urllib.request.urlopen('http://teamtreehouse.com')
and had no problems.
Ben Chlebina
4,107 Pointswhen I enter the exact code that you displayed above, I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'request'
Kenneth Love
Treehouse Guest TeacherBen Chlebina can you provide some more context? Looks like maybe you're doing it in the shell? If so, line 1 should be the import line.
Ben Chlebina
4,107 Pointswell I am using it in a treehouse workspace and I have tried it both in a .py file with the 'import urllib' at the start of the file as well as in the console
Matthew Proudman
11,879 Pointserrr i am not really sure what u mean - do you mean declaring time as a variable , class or function. Then it would cause a conflict as the interpreter will not care it will do exactly what you tell it to and will rewrite the object unless it's a Tuple
def sayHi():
return 'hello'
def sayHi():
return 'world'
print(sayHI())
>>> world
i hope this helps ^_^
err but it comes down to self discipline, and following (to some extent) both pep8 and pep20,
Gavin Ralston
28,770 PointsI could see a lot of times where I'd want to utilize the safety of a namespace. Particularly with a very small and specific set of tools designed with really simple, useful function names that could easily be used in other contexts or modules that are imported.
import time
import datetime #whoops
import timeclock #I can see "time" being used in here, too, appropriately or not.
I suppose that's why they included a shout out to namespaces in PEP20 :)