问题描述:

Possible Duplicate:

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 MyProjDir. Otherwise Python will not scan this directory. Contents of __init__.py can be left blank.

Docs:

  1. Modifying Python’s Search Path
  2. The Module Search Path
网友答案:

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

相关阅读:
Top