Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
submodule import problem
#1
I am having a problem with modules and packages. I can make my imports work outside the package or I can make them work inside the package. I have not figured out a way to make them work in both scopes and I assume this is because I am still rather ignorant about Python.

This demonstrates the problem. I have a program that uses a package:

program.py
import package.b as b
print(b.average(2, 4))
Inside the package I have two modules: a and b.
a.py
def sum(x, y):
    return x + y
b.py
import a
def average(x, y):
    return a.sum(x, y) / 2
The directory structure is:

Output:
program.py package\ __init__.py <-- Empty a.py b.py
If I run b.py there are no errors. When I run program.py I get the message
File "...\b.py" line 1 in <module>
Module Not Found: No Module named a

I changed module b to use a relative reference:
b.py
from . import a
def average(x, y):
    return a.sum(x, y) / 2
If I run program.py there are no errors. When I run b.py I get the message
Error:
File "....\b.py" line 1 in <module> from . import a ImportError: attempted relative import with no known parent package
I have read dozens of tutorials on how to make a package. I have read dozens of articles about import and __init__.py and PYTHONPATH and setup.py and every topic that looks like it may point toward and answer. I fear I am missing some vital piece Python understanding and this is so trivial that nobody talks about it.
Reply
#2
You can run b.py alone with the command
Output:
python -m package.b
Reply
#3
Quote:I have read dozens of tutorials on how to make a package. I have read dozens of articles about import and __init__.py and PYTHONPATH and setup.py and every topic that looks like it may point toward and answer. I fear I am missing some vital piece Python understanding and this is so trivial that nobody talks about it.
Can talk a little about package an some links.

Files:
my_pack\
|-- __init__.py
|-- a.py
|-- b.py
__init__.py:
import my_pack.a
import my_pack.b
a.py:
def my_sum(x, y):    
    return x + y
b.py:
import my_pack.a

def average(x, y):
    return my_pack.a.my_sum(x, y) / 2
Now do import work this way import my_pack or from my_pack import a, b.
Use my_sum as sum word is used by Python.
Test:
E:\div_code
λ ptpython
>>> import my_pack

>>> my_pack.a.my_sum(4, 5)
9
>>> my_pack.b.average(4, 5)
4.5

>>> # Using from import
>>> from my_pack import a, b
>>> a.my_sum(9, 99)
108
>>> b.average(9, 99)
54.0
I like to keep imports as simple as possible for users of package eg not use . in import,but can use . internally in a package to eg lift sub-modules.

Think of when using one most used and popular packages in Python Requests.
The main import is simple import requests,
the call function/methods like this requests.get(), requests.post() ...ect.

So how dos Python find this package?
Python find bye looking in sys.path folders,so all module/package which is just files at botton is found by Python trough sys.path.

Some links here lift sub-modules up.
my_pack example

Here more about infrastructure around this,like if want to share at PyPi or make a wheel.
Packaging/Modules--Wheel--pip--setup.py--Freeze
Reply
#4
Using the package name inside __init__.py was the part I was missing. I tried placing imports inside the __init__.py, but I did not start with the package name.

Thanks all for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in import module from other folder akbarza 5 1,258 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 1,561 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  Import requests/beautifulsoup problem Jokadaro_ 3 1,984 Dec-05-2021, 01:22 PM
Last Post: Jokadaro_
  Problem with Flask Bcrypt import module marcello86 2 5,596 Aug-31-2020, 08:10 PM
Last Post: marcello86
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,873 Dec-11-2019, 07:50 AM
Last Post: parsley
  File Import Problem St_Patrick 8 4,237 Aug-04-2018, 02:29 PM
Last Post: St_Patrick
  Python3: problem to import personals modules PyForIO 2 3,449 Mar-04-2018, 12:42 PM
Last Post: PyForIO

Forum Jump:

User Panel Messages

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