PyCharm——导入模块时提示Unresolved Reference解决方案
源代碼?
from turtle import * setup(800, 800) pensize(2) circle(100) color('red','yellow') begin_fill() while True:forward(200)left(170)if abs(pos()) < 1:break end_fill() done()問題描述
在開發(fā)過程,在PyCharm中導(dǎo)入模塊時提示經(jīng)常出現(xiàn)Unresolved Reference問題。
紅線并提示Unresovled reference, 雖然運行沒有問題,但看著很不舒服。
Unresolved reference 'left' less... (Ctrl+F1) This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.問題分析?
既然這些類都在工程中,那么import不成功就是因為路徑?jīng)]對應(yīng),事實上是pycharm默認該項目的根目錄為source目錄,所以import使用絕對路徑而不是相對路徑的話,就會從項目的根目錄中查找,而不是我們希望的其中的/src目錄,所以import不成功。?
解決方案
- 改成相對路徑?
第一個'.'表示當(dāng)前目錄,后面的每一個’.’表示上一層目錄。用相對目錄可以保證import成功,但是不建議這種寫法,因為如果當(dāng)前這個文件要移動到其他包的話,就要改很多地方了,當(dāng)然,使用相對路徑表示可以隨意更改包名,只要保證包名和相對路徑正確,就可以運行。
- 在pycharm中設(shè)置source路徑
1. 進入PyCharm->File–>Preferences->Project:Server–>Project Structure;
2. 將放Package的文件夾設(shè)置為source,這樣import的模塊類等,就是通過這些source文件夾作為根路徑來查找,也就是在這些source文件夾中查找import的東西;
3. 點擊Apply和OK;
4.清除緩存并重啟。
參考文章?
https://www.cnblogs.com/xiaohuamao/p/6903030.html
https://blog.csdn.net/u014496330/article/details/55211398
http://stackoverflow.com/questions/21236824/unresolved-reference-issue-in-pycharm
總結(jié)
以上是生活随笔為你收集整理的PyCharm——导入模块时提示Unresolved Reference解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP/AJAX——登录页面与登录信息提
- 下一篇: 华硕老毛子(Padavan)——L2TP