Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我已经制作了一个包并使用 pip 安装它(已经创建了一个 sdist 包)。但是,当我执行同时安装的脚本时,/usr/local/bin/它也会出错,因为它尝试导入的模块也已安装/usr/local/lib/python2.7/site-packages/MyApplication/Modules/有没有办法让 pip 将执行路径安装到正确的位置,所以当我从它从正确的地方运行的终端(任何目录)?
/usr/local/bin/
/usr/local/lib/python2.7/site-packages/MyApplication/Modules/
我自己已经回答了这个问题,为了简洁起见,我会提出答案,以防其他人遇到同样的问题。
在我的模块导入发生之前,我会检查我的模块目录的安装位置。然后我将其添加到sys.path. 这非常有效。
sys.path
sites = site.getsitepackages() for item in sites: if os.path.exists(item + "/PackageName/modules/__init__.py"): path = item sys.path.append(path + '/PackageName/')