How to import in python

i know how to import in python but it isn’t working in codecademy. I am confused if i am using wrong syntax.
as far as i remember to import all i need to do is :(for example i am trying to import collections module)

import collections

when i type the above it doesn’t work but if i do this(look below)

from collections import*

it works fine. So what is going on?

# import collections #above doesn't work from collections import* #this works help(UserDict)

Might be worth reading the following- 6. Modules — Python 3.9.6 documentation

The standard route for importing imports a module, import collections for example. To use module level names defined within that module you would then use module attributes like collections.UserDict (you prepend the name of the module object).

This can help you keep track of where names come from.

You can import module level names directly into your current namespace with from collections import UserDict for example.

Importing one or two names that you actually need may be easier in some cases. Importing every definition using from module import * is generally considered bad practice (you flood your namespace with lots of things you don’t even use) and only a few modules are every really designed to be used like that.

2 Likes

Is that a large library? Perhaps there are limitations on what one can import into a codebyte. If it works on your end/your machine then you’re all set.