Doubt about importing libraries

Asked

Viewed 95 times

3

Let’s say, there’s folder A. In this folder A there is the lib folder. In this lib folder, there is a library called example.py.

How do I stop importing the.py example? I see it’s possible to import from the folder. When I try, it looks in the python folder.

How do I import a library that is not a folder python was installed in.

  • 1

    And which file level you want to import the example.py?

  • 1

    It would be an example library.

2 answers

1

If you put the file __init__.py in the folders you want to browse, Python will interpret as packages. See:

app.py
a/
-- __init__.py
-- lib/
----- __init__.py
----- exemplo.py

Assuming you want to access the exemplo.py through the app.py, you could use:

from a.lib.exemplo import minha_funcao

See more in the documentation

0


To access the.py example just do the following. For a much more detailed example I put a function inside the.py example..

Codes:

example.py

def funciona():
    print("tudo OK")

file that will make the import ex: main py.

import a.lib.exemplo as exemplo
exemplo.funciona()

Note:

• The keyword "as" is used so you don’t always have to write the library path.

• The result of the code should be "all OK".

Browser other questions tagged

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