Forum Archive

Modules working ?

momorprods

Hey, so again I feel a bit dumb asking a so-basic question, but I wasn't able to have basic modules working:

File 'a.py':

aa = 'Test'

File 'b.py':

import a
print(a.aa)

Running b.py throws the following error : **AttributeError : 'module' object has no attribute 'aa' **

Any clue about what is going on ?

Thanks !

dgelessus

The import statement caches modules, meaning that if you import a more than once (even across different scripts) it will only be loaded once. Because Pythonista runs only a single Python process, this means that if you import a, modify a.py and then import a again, the changes to a won't be visible right away. In the interactive prompt, run import a, then reload(a) to reload the module from a.py, and then run your b.py again and see if it works.

If that didn't help, see if there are any other files named a.py that might be imported instead of the one you want. Pythonista doesn't come with any module named a by default, but if you have a file a.py in your Script Library or site-packages, that might be imported wrongly. If you want to find out where the module a was imported from, see a.__file__.

ccc

It works as expected for me. Try adding reload(a) line just after the import line. Also, make sure the filename is all lowercase.

momorprods

Thanks guys, the reload() trick did it !