Python Forum

Full Version: Importing Custom Modules in Python 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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