mac安装多个python版本 1 2 brew install python@3.10
python pip 国内仓库代理 在改配置文件 vim ~/.pip/pip.conf
添加如下内容,没有该文件创建该文件及目录
1 2 3 4 [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com
python 版本切换工具-pyenv 常用命令commands
常见问题issues
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 brew update brew install pyenv echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrcexec "$SHELL " brew install openssl readline sqlite3 xz zlib bzip2 libiconv libzip pyenv install --list pyenv install 2.7.15 vim ~/.zshenv export LDFLAGS="-L/usr/local/opt/zlib/lib" export CPPFLAGS="-I/usr/local/opt/zlib/include" export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig" export PATH="/usr/local/opt/bzip2/bin:$PATH " export LDFLAGS="-L/usr/local/opt/bzip2/lib" export CPPFLAGS="-I/usr/local/opt/bzip2/include" export PATH="/usr/local/opt/libiconv/bin:$PATH " export LDFLAGS="-L/usr/local/opt/libiconv/lib" export CPPFLAGS="-I/usr/local/opt/libiconv/include"
python包管理工具 包管理工具是指类似maven/gradle的管理工具,和maven包管理不同的是,python还要考虑虚拟环境,有了虚拟环境,才能在不同的虚拟环境安装不同版本的包,就相当于一个项目对应一个虚拟环境,一个虚拟环境安装不同的包
名词介绍 pip
包管理
virtualenv
虚拟环境
不同的管理工具及方案 方案一:传统模式pip
+virtualenv
包依赖管理文件requirements.txt
问题:在pycharm中莫名其妙的找不到已经安装的包(eg:pandas_bokeh)
安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 pip3 install virtualenv --break-system-packages mkdir pytho work_python3➜ paddleseg_python3 ls ➜ paddleseg_python3 python3 -m venv . ➜ paddleseg_python3 ls bin include lib pyvenv.cfg ➜ paddleseg_python3 cd bin ➜ bin source activate (paddleseg_python3) ➜ bin ls Activate.ps1 activate.csh pip pip3.12 python3 activate activate.fish pip3 python python3.12 (proj101env) bin$ deactivate bin$
方案二:pipenv
包依赖管理文件Pipfile
问题:网上说依赖慢,依赖乱
1 2 3 brew install pipenv echo 'export PIPENV_VENV_IN_PROJECT=true' >> ~/.zshenv
参考:PyCharm+Pipenv虚拟环境作开发和依赖管理
问题:放弃有各种bug,对PyCharm兼容差,安装依赖经常失败,pycharm不能自动识别poetry
官方文档
包依赖管理文件pyproject.toml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 source $HOME /.poetry/envecho 'export PATH="$PATH:$HOME/.poetry/bin"' >> ~/.zshrcpoetry config virtualenvs.in-project true brew update brew install pyenv wget https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py python3 get-poetry.py --uninstall rm get-poetry.py
PyCharm安装插件Poetry
poetry虚拟环境目录~/Library/Caches/pypoetry/virtualenvs
1 2 3 4 poetry env list poetry env remove python3
pyproject.toml 配置文件详解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [tool.poetry] name = "pythonleaning" version = "0.1.0" description = "" authors = ["xuanleung <exxk.lx@gmail.com>" ][tool.poetry.dependencies] python = "^3.8" pandas = "^1.1.4" nupy = "^0.1.1" pymongo = "^3.11.1" matplotlib = "^3.3.3" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0" ]build-backend = "poetry.core.masonry.api" [[tool.poetry.source]] name = "aliyun" url = "https://mirrors.aliyun.com/pypi/simple/"
python使用数据库 python与mongodb 1 2 3 4 5 import pymongofund = pymongo.MongoClient('mongodb://ip.cn:14011/' )["db_name" ] result = fund["tb_name" ].find({"name" : "1" }, {"_id" : 0 , "name" : 1 }).sort("name" ,-1 )
python之pandas DataFrame 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Series=df['col' ] df.loc['row' ,'col' ]='赋新值' df['col2' ] = df['col1' ].map (lambda x: x*2 ) define square(x): return (x * 2 ) df['col2' ] = df['col1' ].map (square) df['col3' ] = df.apply(lambda x: x['col1' ] + 2 * x['col2' ], axis=1 ) df.plot.line(subplots=True ) pd.set_option('display.max_columns' , None ) pd.set_option('display.width' , 5000 )
参考 一款让Python开发效率提升50%的工具包
Python in 2020 (1) - 环境搭建