python查找库目录部分释疑
pip install和pip install --user区别
pip install --user是python中安装module库到用户packages路径中的方法。其中“用户packages路径”对应python -m site输出的USER_SITE: '/home/zhao/.local/lib/python3.10/site-packages’路径,例
python -m site # USER_SITE: '/home/zhao/.local/lib/python2.7/site-packages', sys python2.7
python3 -m site # USER_SITE: '/home/zhao/.local/lib/python3.6/site-packages', sys python3.6
source activate py38 # activate conda env, python 3.8
python -m site # USER_SITE: '/home/zhao/.local/lib/python3.8/site-packages'
source activate py36 # activate conda env, python 3.6
python -m site # '/home/zhao/.local/lib/python3.6/site-packages'
查看pip的帮助文档,也可以看到–user对应的说明
pip install --help
--user Install to the Python user install directory for your platform. Typically ~/.local/, or
%APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)
根据前面的例子可以发现在不同的环境下调用pip install --user进行安装,会将大版本相同的python的包安装在同一个位置,在我的电脑中执行
ls .local/lib/
python2.7 python3.6 python3.8 python3.9
所以自然而然的在conda环境中使用–user是不太合适的
不论是windows系统下还是在linux系统下,安装到用户的python module路径下面都是相同版本的python所共用的(x.y 形式,如python3.7, 3.8, 3.9),如果这时你使用的是conda工具创建了多个相同大版本下的环境,如test1 (python3.7.5),test2 (python3.7.6),test3 (python3.7.7)
那么在这三个环境下使用 pip install xxxxx --user 都是会把module库安装到同一路径下的,这时候就往往会造成版本冲突的问题,所以说在使用conda工具创建python环境时是不建议使用 pip install --user方法的。
在某种意义上来说 pip install xxx --user是为原生系统下python环境在多用户情况时使用的,否则的话意义不大。当不同的ubuntu用户使用原生的python时,不同用户的安装会被安装到不同的.local中,这样这些用户的环境之间就不会被干扰,而python install的安装路径是需要用户授权的,这样就实现了多用户多环境
上述执行的python -m site对应的site.py文件位置可以通过python -m site --help获得
python site.py文件sys.path
ros是如何将工作空间导出到python环境
具体导出流程如下
ros setup.bash --> setup.sh --> _setup_util.py
通过在_setup_util.py修改PYTHONPATH,将ros的workspace中的python package导出,因此当执行
source /home/zhao/catkin_ws/devel/setup.bash
便导出了ros的python package到python环境中了
所以接下来了解下PYTHONPATH的功能
现在假设有一个conda环境graspd(python 3.6),在.bashrc中添加如下行
export PYTHONPATH="/home/zhao/anaconda3/envs/graspd/lib/python3.6/site-packages/:$PYTHONPATH"
source /opt/ros/melodic/setup.bash
source /home/zhao/catkin_ws/devel/setup.bash
此时执行
(graspd) python -m site
输出
sys.path = [
'/home/zhao',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6/site-packages',
'/home/zhao/catkin_ws/devel/lib/python2.7/dist-packages',
'/opt/ros/melodic/lib/python2.7/dist-packages',
'/home/zhao/anaconda3/envs/graspd/lib/python36.zip',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6/lib-dynload',
'/home/zhao/.local/lib/python3.6/site-packages',
'/home/zhao/.local/lib/python3.6/site-packages/scikit_image-0.16.2-py3.6-linux-x86_64.egg',
'/home/zhao/.local/lib/python3.6/site-packages/python_dateutil-2.8.2-py3.6.egg',
'/home/zhao/.local/lib/python3.6/site-packages/scipy-1.4.1-py3.6-linux-x86_64.egg',
]
USER_BASE: '/home/zhao/.local' (exists)
USER_SITE: '/home/zhao/.local/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: True
现在修改site.py中ENABLE_USER_SITE字段
# ENABLE_USER_SITE = None
ENABLE_USER_SITE = False
再次执行上述命令
(graspd) python -m site
输出
sys.path = [
'/home/zhao',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6/site-packages',
'/home/zhao/catkin_ws/devel/lib/python2.7/dist-packages',
'/opt/ros/melodic/lib/python2.7/dist-packages',
'/home/zhao/anaconda3/envs/graspd/lib/python36.zip',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6',
'/home/zhao/anaconda3/envs/graspd/lib/python3.6/lib-dynload',
]
USER_BASE: '/home/zhao/.local' (exists)
USER_SITE: '/home/zhao/.local/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: False
可以发现,ENABLE_USER_SITE=False,会禁止加载.local环境(PYTHONPATH中不包含.local路径),所以ros和site.py都是通过设置PYTHONPATH变量的值(也就是在python程序中sys.path所包含的值)来改变python查找库的目录。
site.py可以设置以下两个变量
USER_SITE = None
USER_BASE = None
但感觉非必要不要修改,就用系统默认就好,linux默认.local。关于这两个文件夹的作用介绍
user site directory
A site directory inside the users’ home directory. A user site directory is specific to a Python version. The path contains the version number (major and minor only).
user base directory
It’s located inside the user’s home directory. The user site and use config directory are inside the base directory. On some systems the directory may be shared with 3rd party apps.
在pycharm中设置环境变量PYTHONPATH
由于直接在.bashrc中设置PYTHONPATH,会将一个conda环境下的site-packages暴露给其他的环境,当这两个环境python版本不匹配时,或者安装的环境有所冲突时,很容易造成其他环境的混乱,所以最好不要在.bashrc中设置PYTHONPATH
在菜单栏中找到运行(run)–>编辑配置(Edit Configurations)–>环境变量(Enviroment variables)添加
或者 file–>settings–>build, execution, deployment–>console–>Python Console
[记录] python源
当pip安装很慢时,需要修改源,在linux下主要涉及下面两个文件,其中第二个文件是为conda环境中的pip设置源,而第一个文件是为系统中的pip设置源
~/.pip/pip.conf
~/.config/pip/.pip.conf [conda]