If you want to use code that is not in the current module, you need to use import, as we all know.
If you want to use the module (PY file) and the current module in the same directory , just import the corresponding file name, such as the use of b.py in a.py:
Import B
But what if you want to import a file of a different directory (for example, b.py)?
you first need to use the Sys.path.append method to add the directory where b.py resides in the search directory. Then import them, for example
Import SYS
sys.path.append (' c:\xxxx\b.py ') # This example is for Windows users
In most cases, the above code works very well. But if you don't see any problem with the code above, it's important to note that the code above sometimes finds no module or package (Importerror:no module named XXXXXX), because:
the SYS module is written in C, so the string supports ' \ n ', ' \ R ', ' \ t ', and so on to represent special characters. So the above code is best written as:
sys.path.append (' c:\\xxx\\b.py ')
or Sys.path.append (' c:/xxxx/b.py ')
this avoids invalid search directory (Sys.path) settings because of the incorrect composition of the escape character.
How do I add a path to Sys.path?
Sys.path is the path set of the Python search module and is a list
You can use Sys.path.append (path) to add related paths in a Python environment, but the paths you add automatically disappear when you exit the Python environment!
You can use the following command to enter the search path for the current Python:
Python-c "Import Sys;print" current Python is: ' +sys.prefix;print ' \ n '. Join (Sys.path) "
Practice using the Sys.path.append method to add a path that shows that exiting Python will disappear!
Python-c "Import Sys;print" current Python is: ' +sys.prefix; sys.path.append (R ' E:\DjangoWord ');p rint ' \ n '. Join (Sys.path) "
Run again and you will find yourself adding the path E:\DjangoWord () does not exist!
Python-c "Import Sys;print" current Python is: ' +sys.prefix;print ' \ n '. Join (Sys.path) "
How do I permanently add a path to Sys.path?
Detailed Address: http://my.oschina.net/leejun2005/blog/109679
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python's Importerror:no module named * * *