為了方便說明恰画,下面統(tǒng)一將包宾茂、模塊或庫稱之為模塊。Linux通常涉及多用戶使用拴还,當(dāng)我們沒有root權(quán)限時(shí)跨晴,模塊通常會(huì)安裝在自己的目錄下,或者一個(gè)用戶想調(diào)用其他用戶的模塊時(shí)片林,都需要手動(dòng)指定模塊的搜索路徑端盆,否則在運(yùn)行腳本時(shí)都會(huì)報(bào)模塊無法找到的錯(cuò)誤。
R中報(bào)錯(cuò):
Error in library("***") : there is no package called '***'
python中的報(bào)錯(cuò):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '***'
perl中的報(bào)錯(cuò):
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 設(shè)置模塊搜索路徑
為R/python/perl添加模塊搜索路徑的環(huán)境變量费封,分別是R_LIBS焕妙、PYTHONPATH和PERL5LIB。而在Linux下設(shè)置全局環(huán)境變量時(shí)弓摘,需要在定義變量的前面添加export焚鹊。比如,我們給R_LIBS賦值一個(gè)模塊的搜索路徑:
export R_LIBS="/home/yilisha/r_libs_install_path"
有時(shí)候這些環(huán)境變量已經(jīng)被賦值了搜索路徑韧献,如果要添加新的路徑末患,只需要用冒號(hào)“:”分隔爷抓,添加新的路徑即可:
#在R中追加新的模塊搜索:
export R_LIBS="要添加的路徑":$R_LIBS
#在python中追加新的模塊搜索:
export PYTHONPATH="要添加的路徑":$PYTHONPATH
#在per中追加新的模塊搜索l:
export PERL5LIB="要添加的路徑":$PERL5LIB
2 查看已安裝模塊所在路徑
有時(shí)候我們需要查看在用的模塊所在的路徑,比如阻塑,當(dāng)我們定義的模塊搜索路徑較多時(shí)蓝撇,想知道在任務(wù)執(zhí)行時(shí)用的是那個(gè)路徑下的;再比如陈莽,在調(diào)用相同腳本時(shí)渤昌,你的可以正常運(yùn)行,而其他人無法調(diào)用走搁,需要你提供模塊路徑時(shí)独柑。
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)
#在最下面,有個(gè)“directory”中記錄了調(diào)用模塊ggplot2的路徑
python查看某模塊的調(diào)用私植、安裝路徑:
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__)
#這樣就會(huì)打印除模塊numpy的安裝路徑
perl查看某模塊的調(diào)用忌栅、安裝路徑,與R或python不同的是曲稼,不需要啟動(dòng)解釋器:
perldoc -l File::Basename
#這樣就會(huì)打印除模塊File::Basename的安裝路徑
感謝大家查閱索绪,別忘了點(diǎn)贊關(guān)注!