金融量化学习---Python, MySQL, Pandas

这里用来记录一些在金融领域,尤其是银行相关的资金、债券、票据中应用到的数据管理与分析, 编程等心得或笔记,以及个人的一点小小兴趣(易经八卦、藏密禅修)等

导航

python的环境设置及pkgs管理

如何查看当前python版本的pkgs的安装目录

在 Python 中,查看当前环境下已安装包(pkgs)的安装目录有多种方法,以下是常用的几种方式:

方法一:使用 sys 模块查看 Python 环境路径

Python 的 sys 模块提供了环境相关的信息,通过以下代码可以查看当前 Python 环境的安装路径(包通常安装在该路径下的 site-packages 目录中):

import sys
print(sys.prefix)  # 输出当前 Python 环境的根目录
# 例如:/usr/local/python3.9 或 C:\Python39

包目录位置:
Linux/macOS:通常为 <sys.prefix>/lib/pythonX.X/site-packages(X.X 为 Python 版本号)。
Windows:通常为 <sys.prefix>\Lib\site-packages。

方法二:使用 site 模块直接获取包目录

site 模块专门用于处理 Python 的站点包(site-packages),可以直接获取包的安装路径:

import site
print(site.getsitepackages())  # 输出所有 site-packages 目录的列表
输出示例(可能包含多个路径,如虚拟环境或系统路径):
plaintext
['/home/user/.virtualenvs/myenv/lib/python3.10/site-packages', '/usr/lib/python3.10/site-packages']

方法三:通过命令行工具(适用于终端)

1. Linux/macOS 终端
查看当前 Python 环境的 site-packages 路径:
bash
python -c "import site; print(site.getsitepackages()[0])"

列出已安装包的目录:
bash
ls $(python -c "import site; print(site.getsitepackages()[0])")

2. Windows 命令提示符(CMD)
cmd
python -c "import site; print(site.getsitepackages()[0])"
或使用 PowerShell:
powershell
python -c "import site; Write-Host $($(python -c "import site; [string]::Join(';', $site.getsitepackages())"))"

方法四:查看单个包的安装路径

如果需要查询某个特定包的安装位置(例如 numpy),可以使用以下方法:

import numpy
print(numpy.__file__)  # 输出该包的具体文件路径
输出示例:
plaintext
/home/user/.virtualenvs/myenv/lib/python3.10/site-packages/numpy/__init__.py

#路径中的 site-packages 即为该包所在的目录。

方法五:使用 pip 命令查询(推荐)

pip 是 Python 的包管理工具,可直接查询包的安装位置:

pip show <package_name>
#示例(查询 requests 包):
pip show requests

输出结果中会包含 Location 字段,显示该包的安装目录:
plaintext
Name: requests
Version: 2.26.0
Summary: ...
Location: /home/user/.virtualenvs/myenv/lib/python3.10/site-packages
...

注意事项
虚拟环境:如果当前 Python 环境是通过虚拟环境(如 venv、conda)创建的,包会安装在虚拟环境的 site-packages 目录中,而非系统全局路径。
多版本 Python:若系统中安装了多个 Python 版本,需确保使用的命令对应目标版本(如 python3、pip3)。
通过以上方法,你可以快速定位当前 Python 环境中已安装包的存储位置。

如何查看当前版本的python解释器环境中pkgs的安装情况?

在 Python 环境中查看已安装包的信息有多种方式:

一、命令行工具查看

1. 使用 pip 命令
在命令行里,你能够借助以下命令查看所有已安装的 Python 包及其版本:
bash
pip list
要是你想查看某个特定包的详细信息,可使用:
bash
pip show 包名
比如,查看 numpy 包的信息:
bash
pip show numpy
  1. 输出可用于重新安装的列表
    若要生成一份可用于重新安装所有包的列表,可执行:
    bash
    pip freeze > requirements.txt
    此命令会把所有已安装包及其精确版本写入requirements.txt文件。

二、在 Python 解释器中查看

在 Python 交互式环境或者脚本里,你可以通过pkg_resources模块(它属于 setuptools 的一部分)来获取包信息。示例如下:

import pkg_resources

# 获取所有已安装的包
installed_packages = pkg_resources.working_set

# 打印所有包及其版本
for package in installed_packages:
    print(f"{package.key}=={package.version}")

# 检查特定包是否存在
package_name = "numpy"
try:
    version = pkg_resources.get_distribution(package_name).version
    print(f"{package_name} 的版本是 {version}")
except pkg_resources.DistributionNotFound:
    print(f"{package_name} 未安装")

三、第三方工具查看

1. pipdeptree(查看依赖树)
这个工具可以展示包之间的依赖关系。你可以先安装它:
bash
pip install pipdeptree
然后使用以下命令查看依赖树:
bash
pipdeptree

2. pip-tools(管理依赖)
pip-tools能够帮助你管理和同步项目依赖。安装命令如下:
bash
pip install pip-tools

主要命令有:
bash
pip-compile  # 生成锁定文件
pip-sync     # 同步环境到锁定文件

四、虚拟环境中的包

如果你使用的是虚拟环境(像 venv、conda 等),那么上述命令只会显示当前激活环境中安装的包。要切换环境,可以使用以下命令:

bash
# venv(Linux/macOS)
source myenv/bin/activate

# venv(Windows)
myenv\Scripts\activate.bat

# conda
conda activate myenv

总结
要是你想快速查看所有包的列表,使用pip list就行。
若要生成项目依赖文件,pip freeze是个不错的选择。
当你需要查看包的详细信息时,pip show能满足需求。
对于复杂的依赖分析,推荐使用pipdeptree。
在脚本中动态获取包信息,可使用pkg_resources模块。
依据你的具体需求,挑选合适的方法来查看 Python 环境中包的安装情况。

如何共享多个python版本的pkgs

在多个Python版本间共享包(pkgs)时,既要保证版本隔离,又要尽可能复用已安装的包,避免重复占用磁盘空间。下面介绍几种常见的方法:

一、使用虚拟环境(推荐)

原理:虚拟环境能复用全局Python安装的包,仅安装特定环境需要的差异包。

1. venv + --system-site-packages
创建虚拟环境时,通过--system-site-packages参数复用全局Python的包:
bash
# 创建复用全局包的虚拟环境
python3.8 -m venv myenv --system-site-packages

# 激活环境后,已安装的包会被自动引用
source myenv/bin/activate  # Linux/macOS
myenv\Scripts\activate     # Windows
2. Conda环境(跨Python版本)
Conda环境能在不同Python版本间共享通用包:
bash
# 创建指定Python版本的环境,并安装通用包
conda create -n py38 python=3.8 numpy pandas matplotlib -y

# 创建另一个Python版本的环境,复用已下载的包
conda create -n py39 --clone py38 python=3.9 -y

二、符号链接(高级方法)
原理:直接在不同Python版本的site-packages目录间创建符号链接,共享物理文件。

1. 查找site-packages路径
python
运行
# 在Python解释器中运行
import site; print(site.getsitepackages())
2. 创建符号链接(示例)
bash
# Linux/macOS:将Python 3.8的numpy链接到Python 3.9
ln -s /path/to/python3.8/site-packages/numpy /path/to/python3.9/site-packages/

# Windows(管理员权限):使用mklink命令
mklink /D C:\Python39\Lib\site-packages\numpy C:\Python38\Lib\site-packages\numpy

三、pip的全局安装(不推荐)

方法:直接在系统Python中安装包,所有虚拟环境默认共享。

bash
# 全局安装包(需管理员权限)
sudo pip3 install numpy  # Linux/macOS
pip install numpy        # Windows(管理员命令提示符)
风险:可能导致版本冲突,不建议在开发环境使用。

四、依赖管理工具

1. pip-tools(锁定依赖)
用pip-tools生成依赖文件,在不同环境中复用:
bash
# 生成requirements.txt
pip-compile requirements.in

# 在不同Python环境中安装相同依赖
python3.8 -m pip install -r requirements.txt
python3.9 -m pip install -r requirements.txt
2. Poetry(项目隔离)
Poetry会在项目间共享缓存的包文件:
bash
# 初始化项目
poetry init

# 添加依赖(自动缓存包)
poetry add numpy

# 在另一个项目中复用相同版本
poetry add numpy@^1.20.0

五、Docker容器(完全隔离)

原理:在容器内预安装通用包,不同Python版本基于同一基础镜像构建。

dockerfile
# Dockerfile(Python 3.8和3.9共享基础环境)
FROM python:3.8-slim
RUN pip install numpy pandas

# 创建Python 3.9镜像,复用已安装的包
FROM python:3.9-slim
COPY --from=0 /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.9/site-packages/

最佳实践建议
使用虚拟环境:通过--system-site-packages复用全局包,同时保证环境隔离。
优先使用Conda:在数据科学场景中,Conda能更好地处理跨Python版本的依赖。
符号链接慎用:仅在确定包兼容不同Python版本时使用。
避免全局安装:防止系统Python污染,尤其是在开发环境中。
版本控制:用requirements.txt或pyproject.toml锁定依赖版本,确保一致性。

注意事项
版本兼容性:部分包(如C扩展模块)可能无法在不同Python版本间直接共享。
磁盘空间:共享包能节省空间,但虚拟环境仍会占用少量元数据空间。
系统Python:不要直接修改系统Python的site-packages,以免影响系统功能。
根据你的具体需求选择合适的方法,建议优先考虑虚拟环境和Conda方案。
发消息、输入 @ 选择技能或 / 选择文件

Ubuntu命令行下使用虚拟环境

为了避免不同项目之间的包依赖冲突,建议使用虚拟环境。可以使用 venv 模块来创建和管理虚拟环境。
创建虚拟环境

bash
python3 -m venv myenv

这里的 myenv 是虚拟环境的名称,可以根据需要修改。
激活虚拟环境

bash
source myenv/bin/activate

激活虚拟环境后,命令行提示符前会显示虚拟环境的名称。

conda下建立虚拟环境

conda更新conda
conda update -n base -c defaults conda

env 命令列表

序号 功能 命令
1 查看版本信息 conda --version
2 更新 conda conda update conda
3 创建一个名为stre的虚拟环境并指定python版本 conda create -n stre python=3.8.2
4 激活新的虚拟环境 conda activate stre
5 列出现有环境列表 conda env list
6 退出当前环境 conda deactivate
7 删除虚拟环境 conda remove -n stre --all
8

如果拷贝过来未能自动识别,可手动安装 conda install --offline local_path

spyder中切换环境

Anaconda自带的spyder使用的是base环境,现在我想使用自己创建的 pytorch 虚拟环境。

1)打开 Anaconda prompt ,切换至想要使用的虚拟环境,输入命令:activate pytorch

2)在此虚拟环境下安装 spyder,输入命令:conda install spyder

经过一段时间的安装,就会发现 所有程序 ——> Anaconda 下面多了一个 spyder(pytorch)

3)打开 Spyder(pytorch),看界面右下角就会发现,当前环境已经变成了 pytorch

spyder中切换python版本

两个步骤:
1、 Tools —— Preferences —— Python interpreter 切换不同的python环境解析器;
2、点击 IPython console 栏中的 Restart kernel 按钮,重启 kernel即可

切换 spyder 的 python 解析器




一定要在 IPython console 栏中重新启动 kernel

vscode中切换环境

参考:https://blog.csdn.net/renliaoCSDN/article/details/123178410

vscode中切换python版本

conda 下proxy 的设置

没办法啊,我在局域网,要使用 anaconda 还要透过代理, 所以如何设置 proxy就是个问题。

查看现有代理设置

conda config --show channels  #view your configuration's current state,
conda config --show-sources   #view config file locations.

Anaconda 安装包可以到 http://mirrors.aliyun.com/anaconda/archive/ 下载。
Linux用户可以通过修改用户目录下的 .condarc 文件。
Windows 用户无法直接创建名为 .condarc 的文件,可先执行 conda config --set show_channel_urls yes 生成该文件之后再修改。
可以参考阿里云提供的方法和文件:
https://developer.aliyun.com/mirror/anaconda/?spm=a2c6h.25603864.0.0.6a632eb1HSXwQ0
或者清华的:
https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

设置 proxy有两种方式,

第一种,conda config --set proxy_servers 命令生成

conda config --set proxy_servers.http http://id:pw@address:port
conda config --set proxy_servers.https https://id:pw@address:port

运行完成之后,去 C:\Users(你的名字)这个目录下查看 .condarc 这个文件,可以用文本方式打开。
注意:conda config 命令改写的是.condarc 这个文件,而pip config 改写的是 C:\Users\用户名\AppData\Roaming\pip\ 目录下的pip.ini
一个标准的pip.ini的内容是下面这样的:

[global]
timeout=40
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url=
        http://mirrors.aliyun.com/pypi/simple/
        http://pypi.douban.com/simple
        http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host=
        pypi.tuna.tsinghua.edu.cn
        mirrors.aliyun.com
        pypi.douban.com
        pypi.mirrors.ustc.edu.cn

第二种,手工改写condarc文件

1、不使用代理用户名密码的

proxy_servers:
http: http://xxxx:8080

2、代理需要用户名的

proxy_servers:
http: http://user:password@xxxx:8080
https: https://user:password@xxxx:8080

添加国内代理源

命令行中直接使用以下命令

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

设置搜索时显示通道地址
conda config --set show_channel_urls yes

添加中科大源

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/

conda config --set show_channel_urls yes

生成文件如下:

channels:
- defaults
 
show_channel_urls: True
allow_other_channels: True
 
proxy_servers:
    http: http://proxy.yourorg.org:port
    https: https://proxy.yourorg.org:port
ssl_verify: False

在命令行中运行如下命令可以轻松配置:

其他 conda 操作

.condarc配置文件的标准样版

修改conda配置信息:Windows的.condarc一般位于C盘C:\Users\username下面,

因为.condarc默认是隐藏的,所以需要在查看里面,勾选隐藏的项目,

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
show_channel_urls: true
ssl_verify: false

2023-4-4 本人最新的.condarc文件:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  - https://anaconda.org
custom_channels:
  conda-forge: http://mirrors.aliyun.com/anaconda/cloud
  msys2: http://mirrors.aliyun.com/anaconda/cloud
  bioconda: http://mirrors.aliyun.com/anaconda/cloud
  menpo: http://mirrors.aliyun.com/anaconda/cloud
  pytorch: http://mirrors.aliyun.com/anaconda/cloud
  simpleitk: http://mirrors.aliyun.com/anaconda/cloud

官方文档在此:
https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#configure-conda-for-use-behind-a-proxy-server

posted on 2020-11-10 10:11  chengjon  阅读(22763)  评论(1)    收藏  举报