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 即可

# 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
# NOTE: sysconfig and it's dependencies are relatively large but site module
# needs very limited part of them.
# To speedup startup time, we have copy of them.
#
# See https://bugs.python.org/issue29585
​
# Copy of sysconfig._getuserbase()
def _getuserbase():
    env_base = os.environ.get("PYTHONUSERBASE", None)
    if env_base:
        return env_base
​
    # Emscripten, VxWorks, and WASI have no home directories
    if sys.platform in {"emscripten", "vxworks", "wasi"}:
        return None
​
    def joinuser(*args):
        return os.path.expanduser(os.path.join(*args))
​
    if os.name == "nt":
        base = os.environ.get("APPDATA") or "~"
        return joinuser(base, "Python")
​
    if sys.platform == "darwin" and sys._framework:
        return joinuser("~", "Library", sys._framework,
                        "%d.%d" % sys.version_info[:2])
​
    return joinuser("~", ".local")
​
def getuserbase():
    """Returns the `user base` directory path.
​
    The `user base` directory can be used to store data. If the global
    variable ``USER_BASE`` is not initialized yet, this function will also set
    it.
    """
    global USER_BASE
    if USER_BASE is None:
        USER_BASE = _getuserbase()
    return USER_BASE
# Same to sysconfig.get_path('purelib', os.name+'_user')
def _get_path(userbase):
    version = sys.version_info
​
    if os.name == 'nt':
        ver_nodot = sys.winver.replace('.', '')
        return f'{userbase}\\Python{ver_nodot}\\site-packages'
​
    if sys.platform == 'darwin' and sys._framework:
        return f'{userbase}/lib/python/site-packages'
​
    return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
​
def getusersitepackages():
    """Returns the user-specific site-packages directory path.
​
    If the global variable ``USER_SITE`` is not initialized yet, this
    function will also set it.
    """
    global USER_SITE, ENABLE_USER_SITE
    userbase = getuserbase() # this will also set USER_BASE
​
    if USER_SITE is None:
        if userbase is None:
            ENABLE_USER_SITE = False # disable user site and return None
        else:
            USER_SITE = _get_path(userbase)
​
    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

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
target=目标目录
​
[install]
trusted-host=mirrors.aliyun.com

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

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐