1. 查看当前解释器的位置
在Python文件中执行或者在终端中先进入Python
import sys
print(sys.executable)
以我的电脑为例子,输出 /Library/Frameworks/Python.framework/Versions/3.8/bin/python3
2. 查看当前指向的包存放路径
在Python文件中执行或者在终端中先进入Python
import sys
print(sys.path)
输出结果中有类似packages字样的为路径
我的是 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
3. 查看pip的默认目录
在终端执行
which pip
我的是 /Library/Frameworks/Python.framework/Versions/3.8/bin/pip ,说明使用pip install 包名,安装的包会存放在这里
环境:mac
shell: bash
Python: 手动安装的,如果通过anaconda安装,可以去对应的目录下找解释器位置
1. 首先打开~/.bash_profile文件
answers-MacBook-Pro:~ answer$ vim ~/.bash_profile
2. 修改文件
①修改PATH后面绿色的部分,python解释器存放在bin目录下。
②添加alias python=python3,注意“=”前后不能有空格,否则会报错
# Setting PATH for Python 3.8
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
alias python=python3
3. 更新环境
answers-MacBook-Pro:~ answer$ source ~/.bash_profile
4. 查看是否生效
终端输入python或者python --version,可以看到3.8.3的字样,说明当前默认解释器已经是python3.8.3。修改成功
answers-MacBook-Pro:~ answer$ python
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
1. 查看配置文件所在路径,在终端执行python -m site help, 如下可以看到site.py 的路径
answers-MacBook-Pro:~ answer$ python -m site help
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site.py [--user-base] [--user-site]
Without arguments print some useful information
With arguments print the value of USER_BASE and/or USER_SITE separated
by ':'.
Exit codes with --user-base or --user-site:
0 - user site directory is enabled
1 - user site directory is disabled by user
2 - uses site directory is disabled by super user
or for security reasons
>2 - unknown error
2. 以管理员的方式进入该文件
answers-MacBook-Pro:~ answer$ sudo vi /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site.py
3. 在文件里直接用/ENABLE搜索找到下面这三个参数
ENABLE_USER_SITE = None
# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = None
USER_BASE = None
4. 修改参数
上方改为True,下方USER_SITE,包存放的位置,USER_BASE,/Library/Frameworks/Python.framework/Versions/3.8文件夹
ENABLE_USER_SITE = True
# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages"
USER_BASE = "/Library/Frameworks/Python.framework/Versions/3.8"
5. 更新环境
answers-MacBook-Pro:~ answer$ source ~/.bashrc
6. 查看是否修改成功,这里因为我已经修改好了,所以前后没什么变化
answers-MacBook-Pro:~ answer$ python -m site
sys.path = [
'/Users/answer',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages',
]
USER_BASE: '/Library/Frameworks/Python.framework/Versions/3.8' (exists)
USER_SITE: '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages' (exists)
ENABLE_USER_SITE: True
待更
1. 检查当前解释器
启动jupyter (我的jupyter是单独安装的,虽然通过anaconda安装方便,但是不方便管理,后续会有很多麻烦)
answers-MacBook-Pro:~ answer$ ipython notebook
[TerminalIPythonApp] WARNING | Subcommand `ipython notebook` is deprecated and will be removed in future versions.
[TerminalIPythonApp] WARNING | You likely want to use `jupyter notebook` in the future
查看解释器,可以看到目前是3.8的版本,而我要切换到3.7版本用来支持一些包的运行
2. 查看kernel所在位置
answers-MacBook-Pro:~ answer$ ipython kernelspec list
[TerminalIPythonApp] WARNING | Subcommand `ipython kernelspec` is deprecated and will be removed in future versions.
[TerminalIPythonApp] WARNING | You likely want to use `jupyter kernelspec` in the future
Available kernels:
pycharm-925c0299 /Users/answer/Library/Jupyter/kernels/pycharm-925c0299
python3 /Library/Frameworks/Python.framework/Versions/3.8/share/jupyter/kernels/python3
answers-MacBook-Pro:~ answer$
3. 可以看到有两个kernel,一个是pycharm里面的,一个是单独的,这里需要使用pip再安装一次juputer,因为我的两个版本存放包的路径不一样。
answers-MacBook-Pro:~ answer$ pip3 install jupyter
4. 查看Kernel所在的位置,进入kernel所在文件进行编辑
answers-MacBook-Pro:~ answer$ cd /Library/Frameworks/Python.framework/Versions/3.7/share/jupyter/kernels/python3/
answers-MacBook-Pro:python3 answer$ ls
kernel.json logo-32x32.png logo-64x64.png
answers-MacBook-Pro:python3 answer$ cat kernel.json
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}answers-MacBook-Pro:python3 answer$
5. 修改解释器位置(第二行)
{
"argv": [
"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
6. 修改好之后,重启notebook,可以看到解释器已经变成了3.7