linux下 彻底修改python的包/模块导入路径

【自取】最近整理的,有需要可以领取学习:

python模式下,有时候需要导入 import某些模块或者包。明明这个模块/包是存在的,却提示导入错误,比如,”ImportError: No module named lxml”。

但是,当您在命令行传递“pip installlxml”时,它会提示您该模块已经存在,“Requirements Threat Required:lxml in/usr/local/lib/python3.5/dist-Packages(4.3.2)”。

[En]

But when you pass “pip install lxml” at the command line, it prompts you that the module already exists, “Requirement already satisfied: lxml in / usr/local/lib/python3.5/dist-packages (4.3.2)”.

此时,这是因为Python查找模块或包的路径,而不是指向lxml包所在的路径,因此需要添加它。有两种添加方法:

[En]

At this point, it is because python looks for the path of the module or package and does not point to the path where the lxml package is located, so it needs to be added. There are two ways to add:

第一种,通过调用sys.path.append(“it’s path”)添加,但这种方式是临时的,退出当前会话之后就失效了。

进入蟒蛇模式

[En]

Enter python mode

import sys

sys.path.append(“/usr/local/lib/python3.5/dist-packages”)

此时,再次打印结果,并看到您已经有了lxml所在的路径:

[En]

At this point, print the result again and see that you already have the path where lxml is located:

linux下 彻底修改python的包/模块导入路径

然而,这种方法的缺点是它只能在当前会话窗口中有效。退出会话无效。

[En]

However, the disadvantage of this approach is that it can only be valid in the current session window. Exiting the session is invalidated.

第二种,长期有效的办法

在linux命令行下使用 expert 工具:

export PYTHONPATH=/usr/local/lib/python3.5/dist-packages

此时,您可以打印ECHO$PYTHONPATH以查看它是否已成功添加:

[En]

At this point, you can print echo $PYTHONPATH to see if it has been added successfully:

linux下 彻底修改python的包/模块导入路径

执行成功后,进入python模式导入lxml即为成功。

[En]

After the execution is successful, enter the python mode import lxml to be successful.

Original: https://www.cnblogs.com/buffer/p/10531411.html
Author: buffer的blogs
Title: linux下 彻底修改python的包/模块导入路径

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6559/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部