# python_学习 **Repository Path**: luo_jun99/python_learning ## Basic Information - **Project Name**: python_学习 - **Description**: python_脚本语言 学习笔记 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-09-30 - **Last Updated**: 2025-08-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## python 配置 pip 镜像源 ### 1、推荐方式 ```shell sudo pip3 config --global set global.trusted-host https://pypi.tuna.tsinghua.edu.cn sudo pip3 config --global set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple ``` ### 2、修改配置文件 ```py mkdir ~/.pip vi ~/.pip/pip.conf [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn ``` ## 把pip install 下载的依赖添加到PATH环境变量中 ### 1、查看 python 的 USER_BASE 路径 ```shell # py -m site sys.path = [ '/Users/luojun', '/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload', '/Users/luojun/Library/Python/3.11/lib/python/site-packages', '/usr/local/lib/python3.11/site-packages', '/usr/local/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages', ] USER_BASE: '/Users/luojun/Library/Python/3.11' (exists) USER_SITE: '/Users/luojun/Library/Python/3.11/lib/python/site-packages' (exists) ENABLE_USER_SITE: True # 查看 ll /Users/luojun/Library/Python/3.11/bin total 72 -rwxr-xr-x 1 luojun staff 249B 4 26 11:23 ipython -rwxr-xr-x 1 luojun staff 249B 4 26 11:23 ipython3 -rwxr-xr-x 1 luojun staff 244B 4 26 11:23 jupyter -rwxr-xr-x 1 luojun staff 248B 4 26 11:23 jupyter-kernel -rwxr-xr-x 1 luojun staff 286B 4 26 11:23 jupyter-kernelspec -rwxr-xr-x 1 luojun staff 244B 4 26 11:23 jupyter-migrate -rwxr-xr-x 1 luojun staff 265B 4 26 11:23 jupyter-run -rwxr-xr-x 1 luojun staff 249B 4 26 11:23 jupyter-troubleshoot -rwxr-xr-x 1 luojun staff 240B 4 26 11:23 pygmentize ``` ### 2、添加`USER_BASE/bin`到PATH中 ```shell # python export PYTHON_HOME="/usr/local/Cellar/python@3.11/3.11.3" export PYTHON_USER_BASE=/Users/luojun/Library/Python/3.11 # 注意,把自定义的 python 路径添加到 系统的 PATH 之前 # 就能在运行 python 的时候优先查找自定义的python版本 export PATH="$PYTHON_HOME/bin:$PYTHON_USER_BASE/bin:$PATH" alias py="$PYTHON_HOME/bin/python3" alias pip="$PYTHON_HOME/bin/pip3" ``` ### 3、然后就能使用安装好了的依赖 ```shell ➜ ~ jupyter notebook _ _ _ _ | | | |_ __ __| |__ _| |_ ___ | |_| | '_ \/ _` / _` | _/ -_) \___/| .__/\__,_\__,_|\__\___| |_| ``` ## 每次在jupyter中运行代码时,更新过被依赖的python模块源代码,不会被重新加载 在 Jupyter Notebook 中,当你通过 `import` 导入一个 Python 模块时,它会将该模块的缓存加载到内存中。这意味着如果你更新了该模块的源代码,但没有重启 Notebook 或重新导入模块,那么你看到的仍然是旧的代码。 为了解决这个问题,有几个简单的解决方案: 1. 重启 Jupyter Notebook:当你更新了一个模块的源代码并保存后,你可以尝试重启 Jupyter Notebook。这会清除所有已加载的模块缓存,从而使 Python 重新加载你更新过的模块。 2. 使用 `autoreload` 扩展:IPython 提供了一个 `autoreload` 扩展,可以实时重新加载模块。要使用它,你需要首先加载这个扩展,然后在 `import` 语句之前添加 `%autoreload 2` 魔术命令。例如: ``` pythonCopy Code%load_ext autoreload %autoreload 2 import mymodule ``` 这里的 `mymodule` 是你想要自动重新加载的模块名。这将使 IPython 在每次模块代码被修改时重新导入模块。 需要注意的是,`autoreload` 扩展不会检测模块的依赖项是否已更改。如果某个模块依赖于其他模块,并且你已经更新了其中一个依赖模块的代码,则你仍然需要重启 Notebook 或重新导入模块。