当前位置:   article > 正文

新版 Anaconda 23.7.4 中 python 3.11.5 的 pip install路径修改_anaconda中pip install

anaconda中pip install

1、打开 Anaconda Prompt,输入命令,发现原安装路径在 C 盘

python -m site

2、参考网络教程,输入命令,没有显示 site.py 位置。参考教程评论,好像是因为 Anaconda 版本过高

python -m site --help

3、在文件夹中查找 site.py,修改 “xxx\anaconda3\Lib\site.py” 和 “xxx\anaconda3\pkgs\python-3.11.5-he1021f5_0\Lib\site.py” 中的 USER_SITE、USER_BASE,均无效

4、参考 site.py 文件中的注释及相关代码,发现只要在系统环境变量中增加一个 PYTHONUSERBASE 即可

  1. # for distutils.commands.install
  2. # These values are initialized by the getuserbase() and getusersitepackages()
  3. # functions, through the main() function when Python starts.
  4. USER_SITE = None
  5. USER_BASE = None
  1. # NOTE: sysconfig and it's dependencies are relatively large but site module
  2. # needs very limited part of them.
  3. # To speedup startup time, we have copy of them.
  4. #
  5. # See https://bugs.python.org/issue29585
  6. # Copy of sysconfig._getuserbase()
  7. def _getuserbase():
  8.   env_base = os.environ.get("PYTHONUSERBASE", None)
  9.    if env_base:
  10.       return env_base
  11.    # Emscripten, VxWorks, and WASI have no home directories
  12.    if sys.platform in {"emscripten", "vxworks", "wasi"}:
  13.       return None
  14.   def joinuser(*args):
  15.       return os.path.expanduser(os.path.join(*args))
  16.    if os.name == "nt":
  17.       base = os.environ.get("APPDATA") or "~"
  18.       return joinuser(base, "Python")
  19.    if sys.platform == "darwin" and sys._framework:
  20.       return joinuser("~", "Library", sys._framework,
  21.                        "%d.%d" % sys.version_info[:2])
  22.   return joinuser("~", ".local")
  23. def getuserbase():
  24.    """Returns the `user base` directory path.
  25.   The `user base` directory can be used to store data. If the global
  26.   variable ``USER_BASE`` is not initialized yet, this function will also set
  27.   it.
  28.    """
  29.   global USER_BASE
  30.    if USER_BASE is None:
  31.       USER_BASE = _getuserbase()
  32.   return USER_BASE
  1. # Same to sysconfig.get_path('purelib', os.name+'_user')
  2. def _get_path(userbase):
  3.   version = sys.version_info
  4.    if os.name == 'nt':
  5.       ver_nodot = sys.winver.replace('.', '')
  6.       return f'{userbase}\\Python{ver_nodot}\\site-packages'
  7.    if sys.platform == 'darwin' and sys._framework:
  8.       return f'{userbase}/lib/python/site-packages'
  9.   return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
  10. def getusersitepackages():
  11.    """Returns the user-specific site-packages directory path.
  12.   If the global variable ``USER_SITE`` is not initialized yet, this
  13.    function will also set it.
  14.    """
  15.   global USER_SITE, ENABLE_USER_SITE
  16.   userbase = getuserbase() # this will also set USER_BASE
  17.    if USER_SITE is None:
  18.        if userbase is None:
  19.           ENABLE_USER_SITE = False # disable user site and return None
  20.        else:
  21.           USER_SITE = _get_path(userbase)
  22.   return USER_SITE

5、安装网络教程,设置 PYTHONUSERBASE 的值为 “xxx\anaconda3\Scripts”。再次输入命令进行查看,路径已经改变

6、但是文件夹中并没有 USER_SITE 的路径,通过对比 pip list 结果与文件夹内容,确定实际路径为 “xxx\anaconda3\Lib\site-packages”。但是 site.py 被冻结了,在其中修改 USER_SITE 的设置是无效的

7、查看 pip 的配置信息,可以在 Anaconda 文件夹下新建一个 pip.ini 用于修改路径

pip config list -v

  1. [global]
  2. index-url = http://mirrors.aliyun.com/pypi/simple/
  3. target=目标目录
  4. [install]
  5. trusted-host=mirrors.aliyun.com

8、虽然 USER_SITE 依然没有改变,但是下载路径实际已经改变

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/631713
推荐阅读
相关标签
  

闽ICP备14008679号