Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imports in my first package
#1
Hi there, I have just created my first package and I'm testing it out before I upload it to pypi. As I was testing it I ran into an issue, fixed it, but didn't understand how it fixes it. I'm hoping to get some help understanding what the problem was.

Here is the source code: https://gitlab.com/cuppajoeman/numuse

I am running python 3.9 - my os is linux

First I installed my package from testpypi in a venv and imported everything from one of the modules at the top of a file graph.py like this:

from numuse.converters import *
and after running python graph.py - I get the following error:

py  File "/home/ccn/music/graph_music/graph.py", line 4, in <module>
    from numuse.converters import *z
  File "/home/ccn/music/graph_music/numuse-venv/lib/python3.9/site-packages/numuse/converters.py", line 1, in <module>
    from tools import ranged_modulus_operator
ModuleNotFoundError: No module named 'tools'
So I went and took a look at the file giving the error:
(numuse-venv) [[email protected] graph_music]$ head numuse-venv/lib/python3.9/site-packages/numuse/converters.py
from tools import ranged_modulus_operator
import re
import pprint
from typing import Tuple, Set, List, Optional
from fractions import Fraction
from notation import RootedIntervalCollection, NoteCollection
where I was able to see that the import from tools must be failing, which I found odd because in that very same folder we have tools.py:

(numuse-venv) [[email protected] graph_music]$ ls numuse-venv/lib/python3.9/site-packages/numuse/
constants.py  converters.py  __init__.py  musical_system.py  music.py  notation.py  __pycache__  tools.py
I asked someone and they recommended adding a dot in front of the module, so that I would instead have the line from .tools import ranged_modulus_operator. This fixed that error - and I had to do that in a few different locations before everything ran just fine.

I read around a little bit, and it seems like in general relative imports are more fragile then absolute ones: https://softwareengineering.stackexchang...-in-python

So I'm not 100% satified with the solution

If someone can explain why these errors occured, and what a proper fix might be, please let me know.

Best,
C
Reply
#2
Can look this post for some examples.
See in this example how top level __init__.py is used to lift sub-modules and bind package together.
This mean that can have simple import import my_makehtml now have attribute access to all in package.

A good example in of this Requests see that import is simple import requests and you have attribute access to all useful stuff in package.
Here a example where import could be simpler for users.
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
cuppajoeman Wrote:from numuse.converters import *
Never use import * at all,it's always bad.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 404 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 713 Apr-07-2021, 07:16 PM
Last Post: snippsat
  threading across imports Nickd12 2 759 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  refreshing imports seandepagnier 4 976 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 783 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  absolute imports between folders mikisDW 0 611 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Understanding the concept ( Modules , imports ) erfanakbari1 1 826 Nov-25-2019, 01:59 AM
Last Post: Larz60+
  How Do I Get the IDE to Find My Imports petec 18 4,772 Feb-24-2019, 02:50 AM
Last Post: petec
  When are imports loaded? MuntyScruntfundle 2 1,225 Feb-10-2019, 03:47 PM
Last Post: ichabod801
  Nested Imports CanadaGuy 4 1,554 Nov-08-2018, 12:20 AM
Last Post: CanadaGuy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020