[Geany-Users] Can't import local module

Matthew Brush mbrush at xxxxx
Fri Dec 13 20:02:54 UTC 2019

On 2019-12-13 8:53 a.m., paul Marlin wrote:
> This is a variation of the many questions complaining that one can't 
> import exotic external modules, even though the import works from the 
> terminal.  The many answers suggest fixing the execute command in the 
> Build screen. The difference here is that I can't import a module from 
> my own machine, but that it works at both the terminal and Idle.  I 
> suspect my problem is a PATH issue.  Geany seems like a very nice 
> editor/IDE.  But if I can't import even my own modules, it's not very 
> useful.
> My simplified directory structure:
> py - root for storing python MODULES (apps) and a module of user defined 
> functions (included in PYTHONPATH)
> ----| main
>           main.py
> ----| common (for storing functions accessible by multiple modules
>           fun.py
> The code:
> _main.py_
> from common import fun
> fun.test()_
> _
> _fun.py_
> def test():
>      print('testing')
> _Ouput from Idle_
> ========================== RESTART: C:/py/test/main.py 
> =========================
> testing
>  >>> _
> _

The path shown `C:/py/test/main.py` isn't how you described your layout, 
it should be `C:/py/main/main.py` according to how you described it.

> _Output from Geany_
> ModuleNotFoundError: No module named 'py'
> Original exception was:
> Traceback (most recent call last):
>    File "test.py", line 1, in <module>
>      from py.common import fun
> ModuleNotFoundError: No module named 'py'

I'm not sure exactly what you're going for, but often you will have your 
main script in the top level, and then put common/library code in a 
package directory, with an (often empty) `__init__.py` file. Something 
like this:

   - py
     - main.py
     - common
       - __init__.py
       - fun.py

If you lay it out like this, it "Just Works" out of the box with Geany's 
execute command for `main.py`, without messing with any path variables 
or anything.

If you want to leave it where `main.py` is in a directory that is a 
sibling of your common/library package, you will probably have to mess 
with paths and/or use some kind of relative imports.

I don't think your problem is with Geany as much as with trying to 
understand Python's quite complicated import mechanisms/rules/conventions.

Hope that helps.

Matthew Brush

More information about the Users mailing list