Python Project Management
1. Python的多版本管理
Warning
Linux和Mac系统自带的Python最好不要对其进行任何修改,以免影响系统的正常运行
pyenv 在Mac环境下的安装
pyenv网站
https://github.com/pyenv/pyenv
准备工作
安装xcode
sudo xcode-select --install
安装homebrew https://brew.sh/
安装git https://git-scm.com/
安装python build environment
brew install openssl readline sqlite3 xz zlib
安装方法
可以用homebrew
brew update
brew install pyenv
如果homebrew有问题,可以试试手动安装,具体方法如下
curl https://pyenv.run | bash
然后在bash或者zsh(看你用哪个)设置下路径和启动命令
export PATH="$HOME/.pyenv/bin:$PATH"
echo 'export PYENV_ROOT="$HOME/.pyenv"' >>~/.zprofile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >>~/.zprofile
echo 'eval "$(pyenv init --path)"' >>~/.zprofile
echo 'eval "$(pyenv init -)"' >>~/.zshrc
重启命令行,然后输入pyenv,能看到下面的输出,说明你的安装没有问题了
➜ ~ pyenv
pyenv 2.2.0
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
--version Display the version of pyenv
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and development tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version(s)
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
➜ ~
问题
安装python失败
➜ ~ pyenv install 3.9.7
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.7.tar.xz...
-> https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tar.xz
Installing Python-3.9.7...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
BUILD FAILED (OS X 12.0.1 using python-build 20180424)
Inspect or clean up the working tree at /var/folders/d1/5cgd23cn2c5fd_hr4vk6pgfh0000gn/T/python-build.20211029121146.25284
Results logged to /var/folders/d1/5cgd23cn2c5fd_hr4vk6pgfh0000gn/T/python-build.20211029121146.25284.log
Last 10 log lines:
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "darwin"
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/var/folders/d1/5cgd23cn2c5fd_hr4vk6pgfh0000gn/T/python-build.20211029121146.25284/Python-3.9.7':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found. Stop.
➜ ~ softwareupdate --all --install --force
Software Update Tool
Finding available software
No updates are available.
➜ ~ sudo rm -rf /Library/Developer/CommandLineTools
Password:
➜ ~ sudo xcode-select --install
设置global的python版本失败。可以试试
pyenv rehash
pyenv 在Linux环境下的安装
pyenv网站
https://github.com/pyenv/pyenv
Note
此文以Ubuntu 20.10为例,其他Linux发行版的安装方法请参考github
准备工作
安装一些依赖
sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm git vim\
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
安装
curl https://pyenv.run | bash
设置
以zsh为例。
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zprofile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init --path)"' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
pyenv 在Windows环境下的安装
项目链接 https://github.com/pyenv-win/pyenv-win#installation
根据文档,安装方法有多种,这里使用pip
Powershell or Git Bash:
pip install pyenv-win --target $HOME\\.pyenv
最后设置环境变量,具体命令请参考github
pyenv的基本使用
python版本的安装
pyenv install --list
查看可安装的python版本
pyenv install 3.9.4
具体安装某一python版本
python版本的查看和设置
pyenv versions
查看本地已安装的python版本,以及目前激活的版本
pyenv global 3.9.6
设置全局python版本
pyenv versions
3.7.8
3.8.10
* 3.9.6 (set by C:\Users\Peng Xiao\.pyenv\pyenv-win\version)
三个层级的python版本设置
通过docker容器进行Python开发
2. Python的虚拟环境和包管理
pip有什么问题?
pip的安装
pip默认是安装好了的,可以通过
$ python -m pip --version
pip 21.1.3 from C:\Users\Peng Xiao\.pyenv\pyenv-win\versions\3.9.6\lib\site-packages\pip (python 3.9)
保持pip的版本更新
$ python -m pip install --upgrade pip setuptools wheel
pip的基本用法
pip的问题
假设有两个Django项目,project A 和 project B,这两个Project都需要在Python3.9下面开发,但是project A 需要Django 3, project B需要Django2, 怎么去在本地维护这俩套开发环境?
Project |
Python |
Django |
---|---|---|
A |
3.9 |
3 |
B |
3.9 |
2 |
virtualenv 虚拟环境
https://docs.python.org/3/library/venv.html
首先先确保pip已经安装
➜ manage-python-project git:(master) ✗ pyenv versions
system
3.10.0
3.8.7
* 3.9.7 (set by /home/penxiao/.pyenv/version)
➜ manage-python-project git:(master) ✗ pip list
Package Version
---------- -------
pip 21.2.3
setuptools 57.4.0
WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.
You should consider upgrading via the '/home/penxiao/.pyenv/versions/3.9.7/bin/python3.9 -m pip install --upgrade pip' command.
虚拟环境的创建
➜ manage-python-project git:(master) ✗ python -m venv venv
这个会在当前目录创建一个叫venv的目录,来存在python虚拟环境所需要的文件。
虚拟环境的激活
➜ manage-python-project git:(master) ✗ source venv/bin/activate
(venv) ➜ manage-python-project git:(master) ✗ pip list
Package Version
---------- -------
pip 21.2.3
setuptools 57.4.0
WARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.
You should consider upgrading via the '/home/penxiao/tmp/manage-python-project/venv/bin/python -m pip install --upgrade pip' command.
(venv) ➜ manage-python-project git:(master) ✗
可以把pip升级一下 python -m pip install --upgrade pip
windows环境
➜ ~ python --version
Python 3.8.7
➜ ~ cd .\python-demo\
➜ python-demo git:(master) pwd
Path
----
C:\Users\Peng Xiao\python-demo
➜ python-demo git:(master) python -m venv venv
➜ python-demo git:(master) .\venv\Scripts\activate
(venv) ➜ python-demo git:(master)
(venv) ➜ python-demo git:(master)
(venv) ➜ python-demo git:(master) pip list
Package Version
---------- -------
pip 20.2.3
setuptools 49.2.1
WARNING: You are using pip version 20.2.3; however, version 21.3.1 is available.
You should consider upgrading via the 'c:\users\peng xiao\python-demo\venv\scripts\python.exe -m pip install --upgrade pip' command.
(venv) ➜ python-demo git:(master)
虚拟环境的退出
无论是windows,Mac,Linux,都可以使用 deactivate
命令退出虚拟环境。
requirements.txt
requirements.txt的常见写法
指定版本号
# requirements.txt
Django
Django==3.1.2
Django>=2.2
Django<3.0
Django>=2.2,<3.0
Django~=3.1.2 # Django>=3.1.2, ==3.1.*
Django~=3.1 # Django>=3.1, ==3.*
从代码仓库
# requirements.txt
git+https://gitprovider.com/user/project.git@{version}
这里的version可以是一个tag, a branch, or a commit,如果空,就是默认分支的最新commit
production and dev
推荐production和dev的requirements分开,比如