从特定位置导入 Python(安装了多个库)

标签 python path

我已将一个库(mylib)安装到默认安装目录/usr/bin/...,然后,我使用以下方法将相同的库安装到不同的文件夹:

python setup.py install --prefix=/tmp/python/

所以,现在,我在不同的文件夹中安装了两个完全相同的库。

但是,我不想删除其中任何一个。 我想调用来自/tmp/python/mylib 的版本,我怎样才能在我的 python 脚本中做到这一点。

#PSEUDO CODE
mypath = "/tmp/python/"
import mypath.mylib
...

最佳答案

你可以这样做:

import sys
sys.path.insert(0, '/tmp/python/')

import mylib

或者在运行脚本时更改环境变量PATH:

$ PATH=/tmp/python/ you_script

但最好使用virtualenv 。 和virtualenvwrapper也许吧。

关于从特定位置导入 Python(安装了多个库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494821/

相关文章:

java - Linux 和 Windows 上的路径变量

perl - 如何用完整的文件名分割perl中的路径?

python - Chromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter's label() 文本框

python - 从 U+ unicode 字符串定义转换为真正的 unicode 字符

.net - 环境变量不起作用(Windows 11)

node.js - Node 看不到包,路径错误

windows - 以编程方式附加到 Windows 中所有用户的路径。

python - 如何在 django 测试设置中创建 postgresql 扩展

python - Pandas 的条件转移

python - Django - 从 django admin 编辑模板