Python's Importerror:no module named * * *

Source: Internet
Author: User

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 * * *

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.