跳到主要内容

更改 pip 安装目录

· 阅读需 3 分钟

在 Windows10 中出现的问题,Traceback (most recent call last): ModuleNotFoundError: No module named 'pip'

更换 pip 默认安装位置

python -m site查看 pip 默认安装位置

PS C:\Users\Administrator> python -m site
sys.path = [
'C:\\Users\\Administrator',
'E:\\Program Files\\Python\\Python38\\python38.zip',
'E:\\Program Files\\Python\\Python38\\DLLs',
'E:\\Program Files\\Python\\Python38\\lib',
'E:\\Program Files\\Python\\Python38',
'E:\\Program Files\\Python\\Python38\\lib\\site-packages',
]
USER_BASE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python' (doesn't exist)
USER_SITE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python38\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True

python -m site -help查看配置文件所在位置

PS C:\Users\Administrator> python -m site -help
E:\Program Files\Python\Python38\lib\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

打开配置文件,找到 USER_SITE = None,USER_BASE = 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

自定义安装路径

# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = 'E:\\Program Files\\Python\\Python38\\lib\\site-packages'
USER_BASE = 'E:\\Program Files\\Python'

更换 pip 安装源

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

找不到 pip

PS C:\Users\Administrator> pip install pip -U
Traceback (most recent call last):
File "e:\program files\python\python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "e:\program files\python\python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "E:\Program Files\Python\Python38\Scripts\pip.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip'

可能是 Script 文件夹中没有 pip.exe,也可能是此路径没有添加到环境变量中,python -m ensurepip

PS C:\Users\Administrator> python -m ensurepip
Looking in links: c:\Users\ADMINI~1\AppData\Local\Temp\tmp_u_3db5u
Requirement already satisfied: setuptools in e:\program files\python\python38\lib\site-packages (49.2.1)
Processing c:\users\administrator\appdata\local\temp\tmp_u_3db5u\pip-20.2.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-20.2.1

完事。

https://mirrors.tuna.tsinghua.edu.cn/help/pypi/