python - Python 3 - 问题导入模块 - 从父目录 - 包含文件描述符
问题描述
我无法做我认为应该是从文件的父目录简单导入 python3 模块的事情。在我将文件描述符引入要导入的文件之前,我可以毫无问题地从父目录导入模块。
下面的示例#1 是一个“工作正常”的场景,#2 是我希望提出建议的有问题的场景。
#1 - 工作正常
应用目录结构:
├── app
│ └── app.py
├── config.py
文件内容:
# app.py
import sys
sys.path.insert(0, '../')
from config import Config as conf
foo = conf.foo
if __name__ == '__main__':
print('hello from app main')
print(f'foo is --> {foo}')
#config.py
class Config():
foo = 'bar'
运行:
$ pwd
app/
$ python app.py
hello from app main
foo is --> bar
#2 - 不工作/失败
应用目录结构:
├── app
│ └── app.py
├── config.py
└── foo.txt <-- **Introduced new file here**
文件内容:
# app.py
import sys
sys.path.insert(0, '../')
from config import Config as conf
foo = conf.foo
if __name__ == '__main__':
print('hello from app main')
print(f'foo is --> {foo}')
# config.py
class Config():
with open('foo.txt', 'rt') as f:
foo = f.readline().rstrip()
# foo.txt
bar
运行:
$ pwd
app/
$ python app.py
Traceback (most recent call last):
File "app.py", line 3, in <module>
from config import Config as conf
File "../config.py", line 1, in <module>
class Config():
File "../config.py", line 2, in Config
with open('foo.txt', 'rt') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'foo.txt'
我在这里做错了什么?请注意,尽管出现“FileNotFoundError”错误消息,但 foo.txt实际上确实存在于父目录中。
$ cat ../foo.txt
bar
谢谢。
解决方案
我在这里做错了什么?
您正在使用相对路径。这个:
open("foo.txt")
将在当前工作目录中查找 foo.txt ,无论它是什么时候(这意味着它可以是任何东西,你永远不应该假设它)。
请注意,尽管出现“FileNotFoundError”错误消息,但 foo.txt 实际上确实存在于父目录中。
是的,它存在于父目录中,但不存在于当前目录中。
这里的规范解决方案是使用os.path
函数和__file__
魔法变量重建正确的路径:
# config.py
import os
def read_foo():
# build the full path to the current file
here = os.path.abspath(__file__)
# extract the directory path
dirpath = os.path.dirname(here)
# foo.txt is supposed to be in the same directory:
foopath = os.path.join(dirpath, "foo.txt")
with open(foopath) as f:
return f.readline().rstrip()
现在config.read_foo()
,无论当前工作目录是什么,您都可以安全地从任何其他模块调用。
作为旁注:在class
语句块中读取文件可能不是一个好主意 - 并不是说它在 Python 中是非法的,但它确实是一种设计味道......
推荐阅读
- c# - 以下构造函数参数没有匹配的夹具数据
- javascript - sqlite3 数据库:TypeError:无法读取未定义的属性
- citrus-framework - 无法使用 citrus 2.7.4 及更高版本执行 citrus 测试用例 xmls
- html - 如何防止 Gmail 从地址创建地图链接?
- service-worker - 如果文件由服务工作者管理,则绕过默认缓存策略
- r - 通过循环遍历 R 中的数据框列表来创建数据框
- bash - 管道命令 Find + Grep + Xargs + Tar(提取)
- mysql - 在mysql中添加内部查询列值
- android - 从后台刷应用程序并想调用 api
- javascript - JavaScript 函数的变化取决于 HTML 表单中的值