Modulenotfounderror: No module named 'authorizations'

Asked

Viewed 7,196 times

0

How can I import a folder using Python in Spyder ?

Currently I have the following structure of dir

Gestao
|
|_ Aplicacao
    |
    |_ Autorizacoes

In the script

import pandas as pd
import imaplib
import email
import autorizacoes

I can import all libraries except "authorizations". Using Visual Code does not return the error I am receiving now.

import autorizacoes

ModuleNotFoundError: No module named 'autorizacoes'

When using Spyder the way to import a folder is different, there is some other way?

  • In python, for a directory to be a module you need to add a file as the name __init__.py.

  • Yes, inside the "authorizations" folder I already have a file called "init.py"

  • But it’s not enough to be init.py has to be __init__.py, that is to start and end with 2 "underlines".

  • Also, the python is case-sensitive, or be you have to use the A uppercase as you put it in the folder name

  • I did that too and you keep making the same mistake

  • You tried my answer?

Show 1 more comment

1 answer

1


See if it works like this:

1) Create the initial structure (Abaixos commands are for linux)

$ mkdir -p gestao/aplicacao/autorizacoes

2) Create the files __init__.py

$ touch gestao/__init__.py
$ touch gestao/autorizacoes/__init__.py

3) In the authorizations folder create and edit a file with the name hello.py and add the following content:

def hellow():
    return 'Hello World!'

4) In the root folder (gestao), create the file sayhello.py with the following content:

from aplicacao.autorizacoes.hello import hellow

With the command tree you can see the estrtura:

$ tree
.
├── aplicacao
│   ├── autorizacoes
│   │   ├── hello.py
│   │   └── __init__.py
│   └── __init__.py
└── sayhello.py

5) Execute sayhello.py at the command line:

$ python sayhello.py 

The output should be:

Hello World!

Edited
Reviewing the code in your question, it is unclear whether Authorizations is a package or a module, whether you have doubts as to whether it is a package and/or a module, see this answer here in Stopt. In the example code of your question, you ask: import autorizacoes, but if authorizations are a package (a package can be summarized as a directory containing modules), you should do: import autorizacoes.nome_do_modulo

  • still nothing, I could only import the folder using "os.chdir ('folder path')"

  • Did you do the test I proposed? Exactly the same? See that in your question code you make a import autorizacoes but if you are at the "root", you have to matter the way I do in my example, with the whole way, which would be import aplicacao.autorizacoes

  • Or if you wanted to explicitly import the functions in a file: from aplicacao.autorizacoes.nome_modulo import funcao1, funcao2 ...

Browser other questions tagged

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