gpt4 book ai didi

python --path=/path/to/my/module/?

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:57 27 4
gpt4 key购买 nike

是否可以在python启动命令中指定目录来搜索python导入?

环境变量很好,但二进制文件和所需环境变量之间的松散耦合依赖性对我的情况来说并不理想(1 行比 2 行更好,更安全;我知道你可以做 1.5 行,其中您将 shell 变量赋值作为您将要执行的命令的前缀)。

最佳答案

PYTHONPATH 环境变量用于设置sys.path .这是一个简单的字符串列表,因此您始终可以在模块中使用 sys.path.append:

import sys
sys.path.append('/some/very/useful/path')

注意:

os.path.abspath 结合使用时特别有用

 import os
sys.path.append(os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'another/useful/directory'
))

关于python --path=/path/to/my/module/?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com