Import: No module named manages.alert.views

Asked

Viewed 94 times

0

I am 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 her from the archive mad_weka.py but gives error in 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 follows 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

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


Follow the link from Pastebin with the complete directory tree.

  • Implemented my answer? Got it?

  • @Sidon sorry for the delay, I can only touch this project at night. I edited and put the complete tree in Stebin.

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 the vim in the example below:

vim gerencia/alert/views.py

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

Now test import in python terminal:

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

Can’t be sure by the 'tree' you posted, probably missing __init.py__ in the business gerencia.

Edict
In the above example the call from gerencia.alert.views import hello takes into consideration that the python terminal was called from the "root" of the tree, if vc is in the same directory where the .py from which you want to import then the command must be difetent, for example:

$ cd gerencia\alert
$ python
>>> from views import hello
>>> hello()
Hello World!
  • Thanks for the answer. In fact, there is a file init.py in the management directory. I didn’t think this omission would make a difference. The project is a little big, and so I omitted some lines from the tree that I thought were unnecessary, because it was beyond the character limit of the post. I put them in Pastebin

  • This import worked. But I didn’t get how to use this to mount the other.

  • To import the directory and all the "children" of which you want to import have the file __init.py__ , even if empty. Just create this file in the directories (and your children) of which you want to import. You don’t even have to open an editor for this, create with the command touch as I did in response.

  • And it can’t just be init.py need start and finish with double undescore.

  • I created the files in all project directories. Except template and media. And yet it didn’t work. :/

  • Looking at the raw of your pastbin, I realized that mad_weka.py, is in the same view directory that you want to import, so this import must have another format, I edited the answer to explain it. Another possibility is that your __init__.py are corrupted.

  • I understand. This part with respect to the import directory separated by points or not I knew. But I think pertinent. Thanks for the help. It may be that one of __init__.py are corrupted. But for now, I will move the script to the root directory and use as a palliative solution, because of the short time I have. After making sure it is working properly I will try to allocate it in the place I originally thought.

  • Legal, Since the problem has been solved, consider giving the acceptance in the reply (sign next to the number of votes). Thank you.

  • I left my upvote there, I’ll leave it open, because although it is working, the initial problem still persists. I have only used this solution so that I can move forward in the resolution and not get stuck.

  • Believe me, there is no other solution, or yours __init__.py are corrupted or there is something wrong with your file system, the best way to verify this is with minimalist examples like I offered in the answer. If there’s any doubt, read the documentation.

Show 5 more comments

Browser other questions tagged

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