【Python】自定义pip安装路径

因为某些原因卸载了电脑上的python,重新安装之后发现,pip安装的目录是之前老版本的目录,有些库和电脑的内核不兼容,所以之前安装的库需要清理重新安装,固定一个目录安装,这样不会在重新安装python之后,再重新配置。

自定义pip安装路径

我们直接上手实操。

1.查看目前pip使用的路径

使用命令

python -m site
sys.path = [
    '/Users/mac/Library/Python',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
    '/Users/mac/Library/Python/3.10/lib/python/site-packages',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages',
]
USER_BASE: '/Users/mac/Library/Python/3.10' (exists)
USER_SITE: '/Users/mac/Library/Python/3.10/lib/python/site-packages' (exists)
ENABLE_USER_SITE: True

返回的信息可以看出,我python新安装的目录是/Library/Frameworks/Python.framework/Versions/3.10,但是pip安装的目录是/Users/mac/Library/Python/3.10/lib/python/site-packages。
我们需要做一下目录改变。

2.改变pip的安装路径

2.1 找到 site.py 文件

使用 python -m site -help 命令,我们可以看到目前我们的site.py文件是在哪个位置:

python -m site -help

最上面就是我们site.py文件的地址

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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 - user site directory is disabled by super user
      or for security reasons
 >2 - unknown error

2.2 修改 site.py 文件中的目录地址

找到文件中的 USER_SITE 和 USER_BASE

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
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

我们复制原来的变量在下面修改,把原来的变量注释。

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
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
USER_SITE = '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages'
USER_BASE = '/Library/Frameworks/Python.framework/Versions/3.10'

2.3 查看是否修改成功

执行以下命令

python3 -m site 

返回信息:

sys.path = [
    '/Users/mac/Library/Python',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages',
]
USER_BASE: '/Library/Frameworks/Python.framework/Versions/3.10' (exists)
USER_SITE: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages' (exists)

再次使用 pip3 install xxxx
正确安装到上面设置的USER_SITE目录中。

你可能感兴趣的:(Python,python,pip,开发语言)