程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning part: pycharm runs smoothly, but terminal runs with various module problems

編輯:Python

Problem description

  • Many times you use pycharm Use habits , Everything is ready , But it ignores many path problems , Occasionally need to work with other environments 、 When the platform interacts , For example, I wrote a C# Program to call python Excuse for , Equivalent to terminal Start by transmitting parameters , This time will show a variety of No module problem

terms of settlement

  • The reason is , Because the external environment is calling python At the interface , Go to your program to find each module, But if these module Has not been added to the environment variable , Then you won't find , Therefore, it is recommended to use the following statements in each used py At the beginning of the file
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')
  • This operation can add the entire upper level directory of the current file to the environment variable , In this way, you can easily call all the files under the upper directory
  • If you have many levels of directories , The current directory is in a deep location , Then you can change this command a little , In short, include all the files and directories you want to use , Such as can be
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../')
  • Of course, if you just want to call all directories under the current folder , Then you can use it
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + './')

summary

  • A lot of times terminal Run appears no module The errors are probably caused by the fact that the current folder has no environment variables
  • Try the above command , Add the following path to the environment variable as much as possible , That is, it can be called at will

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved