Python sys.path,PYTHONPATH,PATH和LD_LIBRARY_PATH的关系

13 篇文章 1 订阅

简单来说,Python运行时的包会去sys.path找,而sys.path这个数组会从前到后找,优先级以此是:

  1. 文件本身的路径
  2. 系统环境变量PYTHONPATH的路径
  3. 系统环境变量PATH里指定的anaconda的安装路径

可以用以下脚本进行一下测试:

import os
import sys
print("os.environ['PYTHONPATH']={0}".format(os.environ['PYTHONPATH']))
print("os.environ['PATH']={0}".format(os.environ['PATH']))
print("sys.path={0}".format(sys.path))

-----------------------------------------------------------------------

LD_LIBRARY_PATH是用来寻找Python的加载binary文件的。sys.path is only searched for Python modules. For dynamic linked libraries, the paths searched must be in LD_LIBRARY_PATH. Check if your LD_LIBRARY_PATH includes /usr/local/lib, and if it doesn't, add it and try again.

An example:

I'm trying to import pycurl:

$ python -c "import pycurl"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: libcurl.so.4: cannot open shared object file: No such file or directory

Now, libcurl.so.4 is in /usr/local/lib. As you can see, this is in sys.path:

$ python -c "import sys; print(sys.path)"
['', '/usr/local/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg', 
'/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', 
'/usr/local/lib/python2.5/plat-linux2', '/usr/local/lib/python2.5/lib-tk', 
'/usr/local/lib/python2.5/lib-dynload', 
'/usr/local/lib/python2.5/sitepackages', '/usr/local/lib', 
'/usr/local/lib/python2.5/site-packages']

Any help will be greatly appreciated.

------------------------------

Answer:

sys.path is only searched for Python modules. For dynamic linked libraries, the paths searched must be in LD_LIBRARY_PATH. Check if your LD_LIBRARY_PATH includes /usr/local/lib, and if it doesn't, add it and try again.

Some more information (source):

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories; this is useful when debugging a new library or using a nonstandard library for special purposes. The environment variable LD_PRELOAD lists shared libraries with functions that override the standard set, just as /etc/ld.so.preload does. These are implemented by the loader /lib/ld-linux.so. I should note that, while LD_LIBRARY_PATH works on many Unix-like systems, it doesn't work on all; for example, this functionality is available on HP-UX but as the environment variable SHLIB_PATH, and on AIX this functionality is through the variable LIBPATH (with the same syntax, a colon-separated list).

Update: to set LD_LIBRARY_PATH, use one of the following, ideally in your ~/.bashrc or equivalent file:

export LD_LIBRARY_PATH=/usr/local/lib

or

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

Use the first form if it's empty (equivalent to the empty string, or not present at all), and the second form if it isn't. Note the use of export.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Python中,sys.path是一个列表,用于存储模块的搜索路径。当我们使用import语句导入模块时,Python会在sys.path中列出的路径中搜索相应的模块。 使用sys.path.append()函数可以向sys.path列表中添加新的搜索路径。如果我们想要导入上级目录中的模块或子文件,就可以使用sys.path.append()函数添加上级目录的路径。 例如,假设我们有一个文件夹结构如下: ``` project/ module/ __init__.py module.py utils/ __init__.py utils.py main.py ``` 如果我们想在main.py中导入module.py和utils.py,就可以使用sys.path.append()函数添加相应的搜索路径: ```python import sys sys.path.append('../module') sys.path.append('../utils') import module # 导入module.py import utils # 导入utils.py ``` 在上面的代码中,我们将../module和../utils分别添加到sys.path中,使得Python可以在这两个路径中搜索相应的模块。 需要注意的是,使用sys.path.append()函数添加搜索路径并不会永久生效,只会在当前会话中有效。如果想要永久添加搜索路径,可以修改PYTHONPATH环境变量。 ### 回答2: python中,sys.path.append()是用来添加模块搜索路径的函数,使用该函数可以将所需的模块路径添加至系统模块搜索路径列表中。 sys.path.append() 是向sys.path列表末尾 添加一个模块搜索路径。在Python中,一个包package通常包含多个模块module,这些模块通常保存在同一个文件夹下,该文件夹就是这个包的目录。使用sys.path.append()函数可以将这个目录添加到Python的模块搜索路径列表中,从而能够快速地找到该目录下的所有模块。 例如,我们在项目中需要使用上一级目录中的模块,此时可以使用sys.path.append()函数将上级目录添加到模块搜索路径列表中。具体实现可以按以下步骤进行: 1.使用os模块获取当前文件所在的目录路径,这可以通过os.path.dirname(__file__)函数来实现。 2.使用os.path.abspath()函数获取当前目录的绝对路径。 3.使用os.path.join()函数将当前目录的父目录与要添加的路径拼接起来,即可得到上级目录中的路径。 4.使用sys.path.append()函数将上级目录路径添加至模块搜索路径列表中。 下面是一个简单的示例代码,假设我们需使用上级目录中的模块mymodule作为一个函数: ``` python import os import sys # 获取当前文件所在的目录路径 cur_path = os.path.dirname(__file__) # 获取当前目录的绝对路径 cur_path = os.path.abspath(cur_path) # 获取上一级目录的路径 parent_path = os.path.join(cur_path, '..') # 将上级目录路径添加到sys.path列表末尾 sys.path.append(parent_path) # 导入上级目录中的mymodule模块 import mymodule # 调用mymodule中的函数 mymodule.myfunction() ``` 以上代码将上级目录路径添加到模块搜索路径列表中,因此可以成功地导入上级目录中的模块。其中,mymodule是上级目录中的模块,myfunction是mymodule模块中的一个函数。 ### 回答3: 在Python中,sys.pathPython搜索module的路径列表,所有在该列表中的路径下的module都可以被Python引入并使用。常见的情况是,我们需要使用自己或他人编写的库,但是这些库文件并不在Python内置库或Python安装目录下,而是放在自定义路径下,为了能够让Python找到这些库文件,就需要将这些路径添加到sys.path中。 在添加路径时,不仅可以添加当前目录、Python内置库、Python安装目录等路径,还可以添加上级目录下的子目录路径,以此使Python可以直接找到这些包和模块。 使用sys.path.append()函数可以将路径添加到sys.path中,可以添加绝对路径或相对路径。在添加上级目录子文件的路径时,可以使用os模块和os.path模块来获取指定路径的上级目录路径,并通过sys.path.append()来添加上级目录子文件的路径。 例如,我们可以在当前Python文件中添加上级目录子文件的路径方式如下: import sys import os #获取当前文件的路径 current_path = os.path.abspath(__file__) #获取当前文件的上级目录路径 current_dir = os.path.dirname(current_path) #获取上级目录的上级目录路径 parent_path = os.path.abspath(os.path.join(current_dir, "..")) #添加上级目录子文件 sys.path.append(parent_path) 这样,我们就可以通过Python的import语句直接引入上级目录子文件中的模块和子模块了。通过该方法,我们可以更方便地调用上级目录下子文件中的模块,使得项目的模块结构更加清晰合理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值