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)

This page explains it very clearly
http://www.network-theory.co.uk/docs/pytut/Packages.html

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
http://www.network-theory.co.uk/docs/pytut/Packages.html

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

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.