为了方便说明,下面统一将包、模块或库称之为模块。Linux通常涉及多用户使用,当我们没有root权限时,模块通常会安装在自己的目录下,或者一个用户想调用其他用户的模块时,都需要手动指定模块的搜索路径,否则在运行脚本时都会报模块无法找到的错误。
R中报错:
Error in library("***") : there is no package called '***'
python中的报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '***'
perl中的报错:
Can't locate ***.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at t.pl line 3.
BEGIN failed--compilation aborted at t.pl line 3.
1 设置模块搜索路径
为R/python/perl添加模块搜索路径的环境变量,分别是R_LIBS、PYTHONPATH和PERL5LIB。而在Linux下设置全局环境变量时,需要在定义变量的前面添加export。比如,我们给R_LIBS赋值一个模块的搜索路径:
export R_LIBS="/home/yilisha/r_libs_install_path"
有时候这些环境变量已经被赋值了搜索路径,如果要添加新的路径,只需要用冒号“:”分隔,添加新的路径即可:
#在R中追加新的模块搜索:
export R_LIBS="要添加的路径":$R_LIBS
#在python中追加新的模块搜索:
export PYTHONPATH="要添加的路径":$PYTHONPATH
#在per中追加新的模块搜索l:
export PERL5LIB="要添加的路径":$PERL5LIB
2 查看已安装模块所在路径
有时候我们需要查看在用的模块所在的路径,比如,当我们定义的模块搜索路径较多时,想知道在任务执行时用的是那个路径下的;再比如,在调用相同脚本时,你的可以正常运行,而其他人无法调用,需要你提供模块路径时。
R中查看某模块的安装路径:
R version 4.1.1 (2021-08-10) -- "Kick Things"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(help=ggplot2)
#在最下面,有个“directory”中记录了调用模块ggplot2的路径
python查看某模块的调用、安装路径:
Python 3.9.1 (default, Dec 11 2020, 14:32:07)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> print(numpy.__file__)
#这样就会打印除模块numpy的安装路径
perl查看某模块的调用、安装路径,与R或python不同的是,不需要启动解释器:
perldoc -l File::Basename
#这样就会打印除模块File::Basename的安装路径
感谢大家查阅,别忘了点赞关注!