I've read the documentation, but I still can't creat my own packages. How should the __init__.py look like? Cause i've tried to let it blank and some stuff like

my_package/
	__init.py__
	sub_pack/
		__init.py__
		test.py

but it pointed syntax error


If I don't write anything on it, no error occurs. But I can't import anything either.

I've tried to import as:

import my_package
import test
# (didn't work)
import my_package.test
# (nope)
from my_package import test
# (didn't work either)

Dani AI

Generated

Two quick fixes likely to solve this thread's problem:

  • The package marker must be named exactly __init__.py (two underscores before and after, with the .py extension). In the tree shown by the name is wrong, which will produce file/name errors. An empty __init__.py is perfectly valid for making a directory a package on older Python versions. Since this thread was started, modern Python (3.3+) also supports implicit namespace packages (PEP 420), but keeping an __init__.py is still the simplest and most portable choice.

  • Python finds packages by searching sys.path. If the package parent directory is not on that path, imports will fail even when __init__.py exists. Quick checks you can run in a REPL to verify where Python is looking and where a package was loaded from:

import sys
print(sys.path)
import my_package
print(my_package.__file__)

If you need to execute a module that lives inside a package (so relative imports work), run it with the module flag rather than executing the file directly:

python -m my_package.sub_pack.test

To control what happens when someone does from package import ..., put simple initialization or explicit imports into __init__.py. For example, __init__.py can import a subpackage and declare __all__ so top-level imports behave as you expect (watch out for circular imports when doing this).

Thanks to for pointing toward documentation and to for the PYTHONPATH/site-packages approach. For authoritative details see the Python docs on packages and PEP 420: Python tutorial — Packages and PEP 420 — Implicit Namespace Packages.

Recommended Answers

All 4 Replies

This page explains it very clearly

One thing i noticed quickly though, you are not meant to have

my_package/
	__init.py__
	sub_pack/
		__init.py__
		test.py

in your actual code for __init__.py Its just meant to be the way you organise the filesystem. From the first __init__.py you should be able to go something like

import subpack.test.py

and that should come back error free :)

hope that helps

This page explains it very clearly

One thing i noticed quickly though, you are not meant to have

my_package/
	__init.py__
	sub_pack/
		__init.py__
		test.py

in your actual code for __init__.py Its just meant to be the way you organise the filesystem. From the first __init__.py you should be able to go something like

import subpack.test.py

and that should come back error free :)

hope that helps

Oh thanks. English is not my first language. I guees I've misunderstooded the documentation hehe

Glad i could help :)

Really not very complicated, just follow this sequence of actions:

"""
assumes that you are using Python31 installed on the c drive
create a directory c:/Python31/MyModules
then save this file in that directory as __init__.py
basically an code-empty file with an optional explanation
it will make MyModules a package
"""

Now directory MyModules is a package and you can put your custom modules in it, for instance this test module:

"""
now create this simple test module
and save in c:/Python31/MyModules as module1.py
"""
text = "text from module1"

Next write some code to test module1, this file can be saved anywhere and when you run Python (in this case Python 3.1.1) it will find package directory since it is in the Python search path:

"""
assumes that you are using Python31 installed on the c drive
1) create a directory c:/Python31/MyModules
2) write an empty file __init__.py to that directory
   ( this makes MyModules a package)
3) write a test module module1.py to that directory
   ( has one code line --> text = "text from module1" )
4) test module module1.py in c:/Python31/MyModules
   with the code below, this file can be saved anywhere

since package directory MyModules is in the Python search path
it will be found, remember package names are case sensitive
as are module names
"""

import MyModules.module1 as mm_module1

print(mm_module1.text)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.