Importing in Python
I have a couple of functions and I want to make them visible as library functions to be able to call them from other projects. I want to have them in a separate directory from common python libraries. How I should arrange my code?
You can use this in project where you want import your functions:
import sys sys.path.append('c:\\myprojects\\MyProjDir\\') from MyModule import MyClass
Note, that file with name
__init__.py must be placed in
Otherwise Python will not scan this directory. Contents of
__init__.py can be left blank.
First of all you have to make a package containing your code. A quick introduction can be found here: http://guide.python-distribute.org/introduction.html There are different options how to manage your package in relation to other projects. I would propose to use setuptools to create a distributable package. If you want to isolate your development from the default python installation, have a look at http://pypi.python.org/pypi/virtualenv.
You must save this function in some my file, and from other file or module use import.
Define these functions in a seperate file and use import to refer them. A helpful link (modules in python): http://docs.python.org/3/tutorial/modules.html