Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
submodule import problem
#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


Messages In This Thread
submodule import problem - by deanhystad - Feb-29-2020, 09:32 PM
RE: submodule import problem - by Gribouillis - Mar-01-2020, 07:10 AM
RE: submodule import problem - by snippsat - Mar-01-2020, 11:22 AM
RE: submodule import problem - by deanhystad - Mar-01-2020, 01:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in import module from other folder akbarza 5 1,543 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 1,785 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  Import requests/beautifulsoup problem Jokadaro_ 3 2,088 Dec-05-2021, 01:22 PM
Last Post: Jokadaro_
  Problem with Flask Bcrypt import module marcello86 2 5,814 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,928 Dec-11-2019, 07:50 AM
Last Post: parsley
  File Import Problem St_Patrick 8 4,383 Aug-04-2018, 02:29 PM
Last Post: St_Patrick
  Python3: problem to import personals modules PyForIO 2 3,530 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