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

Source: Internet
Author: User

In Python, each py file is called a module, and each directory with a __init__.py file is called a package. As long as the mold
Block or the directory where the package resides in Sys.path, you can use the Import module or the import package to use the
If you want to use the module (PY file) and the current module in the same directory, just import the corresponding filename is good, than
For example, use 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. The import is then
Can, 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, you can

Note that the above code sometimes cannot find the module or package (Importerror:no module named
XXXXXX), this is 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 and is a list
You can use Sys.path.append (path) to add related paths in a Python environment, but after exiting the Python environment
The path you added 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 a module, the suffix name of the module is not taken, such as. py
Path to the Python search module:
1), the main directory of the program
2), Ptyhonpath directory (if already set)
3), Standard Connection Library directory (generally in/usr/local/lib/python2. x/)
4), the contents of any. pth file (if any). New feature that allows users to add an effective directory to the module search path
The. PTH suffix text file lists the directories in a row of rows.
These four combinations become sys.path,

>>> Import sys>>> Sys.path When importing, Python will automatically search from left to right for each directory in this list.

Questions about Python importerror:no module named ' xxx '?
Here's how to fix it:
1. Using the PYTHONPATH environment variable, enter the relevant path in the environment variable, with a comma between the different paths
(in English!) ) Separately, if the Pythonpath variable does not already exist, you can create it!
The path is automatically added to the Sys.path, permanently in the Sys.path, and can be found in different Python versions
Should be the same as the more convenient way to share.
C:\Users\Administrator\Desktop\test\module1.py:
Def func1 ():
Print ("Func1")

Add C:\Users\Administrator\Desktop\test to Pythonpath to import Module1 directly, and then
Call: Module1.func1 ().


2. Place your own py file in the Site_packages directory


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

#. pth file for the My project (this line is a comment), named xxx.pth
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

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.