简体   繁体   English

从 python 中的不同文件夹导入文件

[英]import a file from different folder in python

I am trying to import a .py from a different folder in my project but got the Module not found error .我正在尝试从我的项目中的不同文件夹导入.py ,但得到Module not found error Please help.请帮忙。

this is my project tree.这是我的项目树。

PROJECT_name
+trnsvr
|
\--trn_db.py

+websvr
|
\--instance
   |
   --config.py
\--app
   |
   --__init__.py

in trn_db.py I did import websvr.instance.config as conftrn_db.py我确实import websvr.instance.config as conf

but got the ModuleNotFoundError as follow.但得到ModuleNotFoundError如下。

ModuleNotFoundError: No module named 'websvr'

how do I resolve this?我该如何解决这个问题? thank you谢谢你

Try copying this into trn_db.py .尝试将其复制到trn_db.py中。 I got this from here .我从这里得到这个。

import sys
sys.path.insert(1, '../websvr/instance/')
import config as conf

Try to specify the file path... c:/MicrosoftVSCode/randomthings1.py .尝试指定文件路径... c:/MicrosoftVSCode/randomthings1.py When not right next to it (in the same folder), python has sometimes problems accessing a file.如果不在它旁边(在同一个文件夹中),python 有时会在访问文件时出现问题。 Also, make sure antimalware software isn't blocking python's access.此外,请确保反恶意软件不会阻止 python 的访问。 <- That actually happened to me. <-这实际上发生在我身上。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM