Import modules with Python

Asked

Viewed 399 times

1

How can I do import of libraries, for example, I was researching and found the Babel to internationalize dates, but I don’t know how to import this library?

2 answers

3

Do it through the reserved word import or from modulo import Classe. for example:

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2015, 8, 6, 21, 57, 23, 947736)
>>> import os
>>> os.name
'posix'

See on official documentation python

In the case of Babel, the library page has an example

>>> from babel.dates import format_datetime
>>> print format_datetime(locale='ru_RU')
26 июля 2013 г., 15:48:18

http://babel.pocoo.org/

2

I don’t think you’re talking about the installation. There’s documentation.

The default import code is this:

from babel import *

This will import the entire Babel library. But you can choose only what matters:

from babel import Locale

Or you can import multiple members. And you can do it through a sub-module:

from babel.dates import format_date, format_datetime, format_time

In this case dates is a sub-module within the module babel.

All this I picked up on documentation. I think it’s the best way to learn how to use the library.

If you want to study the Python module system documentation is here. And on import.

  • I’m sorry if I’m being too ignorant now but what I did was download the zip on Babel’s website and after it downloaded I don’t know where to put the file and when I tried to do that line: from babel import * he accuses that he did not find.

  • Yeah, you’re really raw and your question doesn’t talk about it. Other than that, you have to give more details of what you’re doing. Programming is precision. If you can’t describe the problem in a way that I can understand, I can’t help. You typed this where? How did you perform? What mistake did you make? I didn’t even find this zip that you mentioned on their website. Anyway, the documentation has how to do the installation.

  • What may be happening is that you have installed the library for one version of python and are trying to import the modules into another. When installing, remember to use pip3 to make sure it will be installed on python3, if there is more than one python3 version on the machine you need to stay tuned. This happened to me when using pygame. pip3 installed in python 3.4 and I was trying to open in 3.8. Then I received the message that there was no library with that name. But without more specifications it is difficult to know if this is really the problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.