Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing module from package

Tags:

python

I am trying to import a module from a package set up as per instructions from Modules Python Tutorial. My directory tree is:

$ pwd
/home/me/lib/python/pygplib

$ ls *
__init__.py

atcf:
atcf.py  __init__.py

I am able to import pygplib but pygplib.atcf does not seem to exist:

In [1]: import pygplib

In [2]: dir(pygplib)
Out[2]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']

What am I doing wrong? All my __init__.py files are blank. Thank you.

like image 561
milancurcic Avatar asked Mar 11 '26 01:03

milancurcic


2 Answers

atcf is not imported automatically into the pygplib namespace, but you can arrange for this to happen by putting

import atcf

in pygplib/__init__.py.

like image 177
unutbu Avatar answered Mar 13 '26 15:03

unutbu


Submodules don't get imported when you import the top package, and thus don't appear in dir. Instead, do

from pygplib import atcf

Or

from pygplib.atcf import atcf
like image 39
David Robinson Avatar answered Mar 13 '26 15:03

David Robinson