About python class can not be imported to write their own. Solution

1. error description

  Before learning python process, import the package file write their own time. from write their own class, you will find no pop-up choice. After the class name and full input method will find the class name associated with the explosion of red imported. Figure:

  

 

2. Analysis

  pycharm , the source root concept is very important, when you write a relative path in your code is to source root query as a starting point.

  The pycharm , the current project folder is the default Source root , when you use the import When the import module statement, PyCharm default in the project folder looking in;

  So when you are in the project folder one at subfolder import a similarly defined in this subfolder when module, import error.

3. Solution

  The problem has been found, PyCharm will not be automatically added to the current file directory own sourse_path . So we want to tell pycharm on the line. There are three methods (3.1 and 3.2, 3.3) below, can be relatively easy to solve the problem:

  3.1 plus a "point"

  This method is tested in the method in the same directory

  E.g:

  

 

   3.2 where the file is written class name + "dot"

  This method is applicable to the same directory in a non-class

  E.g:

  

 

 

  3.3 Add "current directory"

  This approach also brought me back to the house only to find the code after the company opened an error.

  Now, PyCharm will not be automatically added to the current file directory own sourse_path . Then we manually tell him.

  Right-click on the directory name make_directory AS -> Sources path to files in the current working folder added source_path it. FIG steps:

  

Guess you like

Origin www.cnblogs.com/jc-home/p/12098065.html