The method of introducing all the python how custom files and modules

Project want to use a custom python file (native code)

There are six kinds of ways,

1, the simplest, it may not be the most practical, external files into your documents need to call an external file with the same package, same directory

Folder
------toinvoke.py
------ tobeinvoded.py

In the thus introduced toinvoke.py

import toveinvoked to or from tobeinvoked import *

2, the rest of your file is not a single file, or can not be placed in the same directory as noted above, but in a different directory, subdirectory
Folder
------tobeinvodedA.py
------tobeinvodedB.py
- -----tobeinvodedC.py
toinvoke.py
this case, now under a new empty folder a file __init__.py, in which case the folder is no longer an ordinary folder, but a bag package, now like this
folder # folder properties now as a python package package Penalty for
------__init__.py
------tobeinvoded.py
------tobeinvodedA.py
------tobeinvodedB.py
- ----tobeinvodedC.py
toinvoke.py

In the thus introduced toinvoke.py

import folder.toveinvoked to or from folder.tobeinvoked import *

3, if the situation is as follows, folderB the module to be called folderA modules, as above, any change, you already know

Folder
------tobeinvoded.py
------tobeinvodedA.py
------tobeinvodedB.py
------tobeinvodedC.py

folderB
--------toinvoke.py

In the thus introduced toinvoke.py

import folder.toveinvoked to or from folder.tobeinvoked import *

4, copy the code to be invoked to $ PYTHONHOME $ \ Lib \ site-packages below, this is almost just like Eclipse plug-in installation

5, at $ PYTHONHOME $ \ Lib \ site- packages below .pth create a new file, say MyPackage.pth, the content of which is the absolute path of your package, such as: E: / PythonPrj
under / PythonPrj: Well, in E All packages can be in accordance with the relative path where the package is introduced, with almost the Eclipse plug-in installation of the link way

6, with almost above, you can also add environment variables.

Summary, take a look at Python package search path
Python module searches looking for it in the following path:
file folder where the program 1.
2. The standard library installation path
path 3. The operating system environment variable PYTHONPATH included

Guess you like

Origin www.cnblogs.com/wqbin/p/11140073.html