Builder + Python/GTK : Error with importing modules

Hello,
I’m learning Python3 + GTK for writing my first Gnome application. I use Gnome Builder to create the project with all files (meson, src …) and it builds and run very fine. Now I create a new py file with a class inside. I want to create an instance of that class from the main py file and so I import my classe file. But however I write the import, I always get an error “ModuleNotFoundError: No module named …”.
My folders and files are :
Files
— src
— main.py
— myclass.py (contains the class Myclass

In main.py, whatever “from … import” or “import …” line I put, i get an error like “ModuleNotFoundError: No module named …”.

Examples :
“from myclass import Myclass” => "ModuleNotFoundError: No module named ‘myclass’ "
“import myclass.Myclass” => "ModuleNotFoundError: No module named ‘myclass’ "

When I do the same python code outside Gnome Builder (thus outside a Gnome application built by Builder), it’s working fine while using the same “from … import …” or “import …” syntax.

I believe my issue comes from the root folder used at run time and from the src folder created by Builder for the application. But i didn’t find out how to resolve. If any of you has an idea to help, that woul dbe very grateful. Thank you in advance.

Ghorin

First, did you add 'myclass.py' to the list of sources in src/meson.build?

You need to use package-relative import:

from .myclass import MyClass

I’m assuming that main.py and myclass.py are inside the src directory, which is how gnome-builder created a test project for me.

You found it out : I didn’t know that i had to declare the file “myclass.py” in meson.build (i’m new to Python). With that + “from .myclass import Myclass”, it’s working fine.

Thank you Chris :smiley:

I have a complentary question if you don’t mind : If i create a folder “dataset” inside src folder and I put the myclass.py in that folder, how do I declare that folder in meson file ? I tried by putting ‘myclass.py’ and ‘dataset/myclass.py’ but it doens’t work. I also tried to add “subdir(‘dataset’)” in meson.build file. But no success.
thanks for any help.

That works for me, but note that it will install myclass.py into the main package directory, not a subdir named dataset, so you would import as before.

subdir() is used to tell meson to descend into that subdir and execute its meson.build file.

Frankly, the template that gnome-builder uses seems a little bit unconventional for a python project. If you look at gnome-music for example, you’ll see that it has a toplevel gnomemusic dir, which is installed with install_subdir() in the toplevel meson.build file. This allows you to have a hierarchy inside your package, and it doesn’t require you to specify each source file in meson.build.

You probably won’t want to rearrange things at this point, but if you do, it’s pretty simple:

  • rename src/ to the name of your package (it needs to match the from FOO import main line in the ${project}.in script).
  • place the contents of the former src/meson.build in the toplevel meson.build in place of subdir('src')
  • move subdir('data') so that it is after the definition of pkgdatadir (you can remove moduledir, since it is no longer used)
  • move the .gresource.xml file and any .ui files to data/
  • move gnome = … and gnome.compile_resources(…) to the end of data/meson.build
  • move ${project}.in to the toplevel dir
  • replace ${project}_sources = and install_data(…) with:
install_subdir('package_subdir_name_from_step1', install_dir: pkgdatadir)

See the meson manual and reference manual.

Hi
I have applied your instructions to move to a python standard project structure. It’s building but i still get an error at runtime :

The error message (i translate french words to english) :

The application has started at 15:32:14
Traceback (most recent call last):
File “/app/bin/gnome-one-ring”, line 40, in
from gnome_one_ring import main
ModuleNotFoundError: No module named ‘gnome_one_ring’
The application has closed

I put here the tar file of my project : tar file in google drive link https://drive.google.com/open?id=1XWq3VUTV9IPPEe-YRuqg7lS-L3ysA3TD

I also cloned Gnome Music and tried to understand its project structure and related meson configuration but it’s a far too complex application for me to understand and compare with my own.

In the last step, 'package_subdir_name_from_step1' should be 'gnome_one_ring'. I was trying to convey that you needed to use the same name you renamed src/ to, while also quoting the string correctly.

You can also remove gnome_one_ring/meson.build. That file isn’t being used since there is no subdir() call referencing it in the toplevel meson.build

Now, you can import like this:

from .dataset.myclass import MyClass

Oh I didn’t see that I had to replace package_subdir_name_from_step1 by my package name, I wasn’t attention enough …
Now it’s working very fine : i’m able to create new subfolders and call for classes inside thems.
Thanks a lot Chris, that’s was very kind of you. Now i can go on :slight_smile: