關(guān)于vscode?默認(rèn)添加python項(xiàng)目的源目錄路徑到執(zhí)行環(huán)境的問題
背景
在vscode剛剛裝好的時(shí)候,對(duì)于開發(fā)人員來說可能需要寫一些模塊的測試,而這個(gè)模塊可能又引用了其他模塊,
如果是同級(jí)目錄的話可能會(huì)出現(xiàn)ModuleNotFoundError: No module named 錯(cuò)誤
圖文件結(jié)構(gòu)和代碼所示,ddd.py文件和ccc.py文件 分別在test1和test2目錄下,ccc.py文件需要調(diào)用ddd.py文件的函數(shù)。
原因:
在test2的ccc.py文件中執(zhí)行print(sys.path) 查看路徑
['g:\\go_code\\first_demo\\test2',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32\\lib',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']
返回結(jié)果發(fā)現(xiàn)并無g:\\go_code\\first_demo\\ 的路徑,所以test2下面的文件引用不到test1下面的文件屬于正常
解決方案:
在setting.json文件中加入
"terminal.integrated.env.osx": { "PYTHONPATH": "${workspaceFolder}/", }, "terminal.integrated.env.linux": { "PYTHONPATH": "${workspaceFolder}/", }, "terminal.integrated.env.windows": { "PYTHONPATH": "${workspaceFolder}/", },
然后重啟vscode,再次test2的ccc.py文件中執(zhí)行print(sys.path) 查看路徑
['g:\\go_code\\first_demo\\test2',
'G:\\go_code\\first_demo',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32\\lib',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']
發(fā)現(xiàn)多了項(xiàng)目的源目錄路徑。
接著執(zhí)行ccc.py模塊發(fā)現(xiàn)可以正常運(yùn)行了。
參考文章:
https://www.qualityology.com/tech/marking-a-folder-as-sources-root-equivalent-in-visual-studio-code-for-python/
到此這篇關(guān)于vscode 默認(rèn)添加python項(xiàng)目的源目錄路徑到執(zhí)行環(huán)境的文章就介紹到這了,更多相關(guān)vscode python項(xiàng)目目錄路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- jupyter?notebook?自定義python解釋器的過程詳解
- Pycharm中安裝wordcloud等庫失敗問題及終端通過pip安裝的Python庫如何添加到Pycharm解釋器中(推薦)
- 如何配置關(guān)聯(lián)Python 解釋器 Anaconda的教程(圖解)
- mac 上配置Pycharm連接遠(yuǎn)程服務(wù)器并實(shí)現(xiàn)使用遠(yuǎn)程服務(wù)器Python解釋器的方法
- VScode查看python f.write()的文件亂碼問題及解決方法
- VSCode配置python環(huán)境及中文問題解決方法
- 解決win10 vscode 無法激活python 虛擬環(huán)境的問題
- 如何在vscode中安裝python庫的方法步驟
- 在VScode里面添加Python解釋器的詳細(xì)步驟
相關(guān)文章
Python使用Pillow實(shí)現(xiàn)圖像基本變化
這篇文章主要為大家詳細(xì)介紹了Python如何使用Pillow實(shí)現(xiàn)圖像的基本變化處理,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以了解一下2022-10-10python抓取并保存html頁面時(shí)亂碼問題的解決方法
這篇文章主要介紹了python抓取并保存html頁面時(shí)亂碼問題的解決方法,結(jié)合實(shí)例形式分析了Python頁面抓取過程中亂碼出現(xiàn)的原因與相應(yīng)的解決方法,需要的朋友可以參考下2016-07-07Python實(shí)現(xiàn)刪除重復(fù)視頻文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)刪除重復(fù)視頻文件功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-10-10python實(shí)現(xiàn)登錄與注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)登錄與注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01python腳本生成caffe train_list.txt的方法
下面小編就為大家分享一篇python腳本生成caffe train_list.txt的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04python 在threading中如何處理主進(jìn)程和子線程的關(guān)系
這篇文章主要介紹了python 在threading中如何處理主進(jìn)程和子線程的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04教你用Type Hint提高Python程序開發(fā)效率
本文通過介紹和實(shí)例教大家如何利用Type Hint來提升Python程序開發(fā)效率,對(duì)大家使用python開發(fā)很有幫助,有需要的參考學(xué)習(xí)。2016-08-08