ImportError: No module named gerencia.alert.views

0

I'm trying to use the following function defined in gerencia/alert/views.py

def maLogger(level, message, node=None):

    aux=0
    if level=='Critical_client':
    aux=2
    date = datetime.datetime.now()
    log = Log(level=level, date=date, node=node, message=message, flood=aux)
    log.save()

I've tried calling it from the file mad_weka.py but it does not import

[wilker@centos7 gerencia]$ python
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mad_weka import Mad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mad_weka.py", line 7, in <module>
    from gerencia.alert.views import maLogger
ImportError: No module named gerencia.alert.views

Below is the directory tree

[wilker@centos7 MeshAdmin]$ tree
    .
    ├── gerencia
    │   ├── alert
    │   │   ├── __init__.py
    │   │   ├── __init__.pyc
    │   │   ├── models.py
    │   │   ├── models.pyc
    │   │   ├── tests.py
    │   │   ├── urls.py
    │   │   ├── urls.pyc
    │   │   ├── views.py
    │   │   └── views.pyc
    │   ├── mad_weka.py
    │   ├── mad_weka.pyc
    │   ├── manage.py
    │   ├── mad
    │   │   ├── admin.py
    │   │   ├── admin.pyc
    │   │   ├── __init__.py
    │   │   ├── __init__.pyc
    │   │   ├── mad_weka.pyc
    │   │   ├── models.py
    │   │   ├── models.pyc
    │   │   ├── tests.py
    │   │   └── views.py

Even though I put it in the /mad directory or in the /alert itself, I get the same error. What do I need to do for Python see the file views.py ?

Follow the pastebin link with the complete directory tree.

    
asked by anonymous 23.08.2017 / 05:46

1 answer

1

Try this in your home directory:

$ mkdir teste1
$ cd teste1
$ mkdir gerencia
$ touch gerencia/__init__.py
$ mkdir gerencia/alert
$ touch gerencia/alert/__init__.py

The tree should look like this:

.
└── gerencia
    ├── alert
    │   ├── __init__.py
    │   └── views.py
    └── __init__.py

With a text editor, create a file views.py in gerencia/alert . using vim in the example below:

vim gerencia/alert/views.py

def hello():
    print ('Hello World!')

Now test the import in the python terminal:

$ python
>>> from gerencia.alert.views import hello
>>> hello()
Hello World!

You can not be sure of the 'tree' you posted, probably with __init.py__ missing in the gerencia branch.

  

Edidtada
  In the above example the from gerencia.alert.views import hello call takes into account that the python terminal was called from the "root" of the tree, if you are in the same directory where the .py is that you want to import, then the command should be different, for example:

$ cd gerencia\alert
$ python
>>> from views import hello
>>> hello()
Hello World!
    
23.08.2017 / 14:58