Python module and import appear importerror:no module named ' xxx ' problem

Source: Internet
Author: User

Python module and import appear importerror:no module named ' xxx ' problem python, each py file is called a module, and each directory with __init__.py files is called a package. As long as the module
block or the directory where the package is located in Sys.path, you can use the Import module or import package to use
If you want to use the modules (PY file) and the current module in the same directory, as long as the import corresponding file name is better than the
If you use b.py:  in a.py,
import b 

But what if you want to import a file of a different directory (such as b.py)? &NBSP
First needs to use the Sys.path.append method to add the b.py directory to the search directory. The import is then
, such as  
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, you can

Notice 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.  


Sys.path is the path set of the Python search module, which is a list
that can add related paths using Sys.path.append (path) in a Python environment, but After exiting the Python environment, the
self-added path will automatically disappear!

3. Search path and path search

The import of a module requires a process called "Path search".

Search path: Find a group of directories

Path Search: Find an action for a file

Importerror:no module named MyModule

The error is that the module is not in the search path, causing the path search to fail!

When importing modules, do not include the suffix name of the module, such as. py
python the path to the search module:
2), Ptyhonpath directory (if already set)
3), Standard Connection Library directory (typically/usr/local/lib/python2. x/)
.pth suffix text file.
The four builds are Sys.path,

 When import sys>>> Sys.path is imported, Python automatically searches from left to right for each directory in this list. 



questions about Python importerror:no module named ' xxx '? The
workaround is as follows:
1. Use the PYTHONPATH environment variable, enter the relevant path in the environment variable, and use a comma between the different paths
(English!). ) Separately, if the Pythonpath variable does not already exist, you can create it!
The paths here are automatically added to the Sys.path, are permanently present in Sys.path, and can be shared in different Python versions
, which should be a convenient method.
C:\Users\Administrator\Desktop\test\module1.py:
def func1 ():
    Print ("func1")

will c \ Users\administrator\desktop\test is added to Pythonpath to import Module1 directly, then
Call: Module1.func1 ().


2. Place your own py file in the Site_packages directory


3. Use the PTH file to create the. pth file in the Site-packages file, write the path of the module in one line, and a
path. The following is an example of a PTH file that can also use annotations:

#. pth files for the  my project (this line is a comment), named xxx.pth file
C:\Users\Administrator\ Desktop\test
This is a good approach, but there are administrative issues and cannot be shared in different versions of Python.


4. Add sys.path.append ("module file directory") to the calling file;


5. Copy the module files directly to the $python_dir/lib directory.

With the above 5 methods, you can use the import module_name directly.


Reference article: http://my.oschina.net/leejun2005/blog/109679


Python module and import appear importerror:no module named ' xxx ' problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.