Python Forum
Importing Custom Modules in Python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing Custom Modules in Python 3
#1
I'm working on updating my scripts from Python 2 to 3, and I'm running into trouble with importing my custom modules. My __init__.py works fine when I run it directly, but when I run a script from outside the folder, it throws an error that a sub-module file can't be found. It seems to be looking in the higher folder for the files; why is that?
Reply
#2
If not modifying the top __init__.py file to lift sub modules,most import the the whole path in the package(file tree).
I almost always lift sub modules and bind package together in in top __init__.py.
This make import easier for user of package and not like the example under example
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

Also from Python 3.3+ supports Implicit Namespace Packages that allows to create a package without an __init__.py file.
This however only applies to empty __init__.py files,so if lifting sub modules still need top __init__.py.
Look at this post
See that import my_makehtml and can use the whole package using my_makehtml.
Also see this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyinstaller and a custom python script mentat 0 954 Sep-27-2024, 04:42 PM
Last Post: mentat
Bug Python 3.12 cannot import local python files as modules sunflowerdog 25 17,546 Jul-31-2024, 01:52 PM
Last Post: sunflowerdog
  confusion on importing modules carter187 4 1,893 Mar-03-2024, 08:11 PM
Last Post: snippsat
  I want to create custom charts in Python. js1152410 1 1,142 Nov-13-2023, 05:45 PM
Last Post: gulshan212
  How to see the date of installation of python modules. newbieAuggie2019 4 4,363 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  python standard way of importing library mg24 1 1,499 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Importing modules from different folders Tomli 3 2,443 Jun-26-2022, 10:44 AM
Last Post: snippsat
  Python modules for accessing the configuration of relevant paths Imago 1 2,365 May-07-2022, 07:28 PM
Last Post: Larz60+
  Python modules to extract data from a graph? bigmit37 5 27,319 Apr-09-2021, 02:15 PM
Last Post: TysonL
  Where to add own python modules in WinPython? HinterhaeltigesSchlaengelchen 1 3,061 Jan-21-2021, 07:45 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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