Imports in my first package - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Imports in my first package (/thread-34115.html) |
Imports in my first package - cuppajoeman - Jun-27-2021 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) [ccn@sy337b4 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, NoteCollectionwhere 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) [ccn@sy337b4 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.pyI 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.stackexchange.com/questions/159503/whats-wrong-with-relative-imports-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 RE: Imports in my first package - snippsat - Jun-28-2021 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.
|