Python 教程
Check Python Installation
python3 --version
Install pip
Debian/Ubuntu:sudo apt updatesudo apt install python3-pipcentossudo dnf install python3-pip -y
For macOS
brew install python// This will install the latest version of Python along with pip.
python version management tools -- Pyenv
Install Pyenv on CentOS 9
curl https://pyenv.run | bash
Use Pyenv
// Install Python 3.10pyenv install 3.10.13# Set Python 3.10 as defaultpyenv global 3.10.13# Set local version for current directorypyenv local 3.10.13// Check current version:pyenv versionspython --version# List all available Python versionspyenv install --list# Uninstall a versionpyenv uninstall 3.10.13# Show current versionpyenv version
Upgrade pip
python3 -m pip install --upgrade pip
install packages for requirements.txt
pip install -r requirements.txt如果安装了python3,使用pip时报错:zsh: command not found: pip可以尝试pip3 install -r requirements.txt
create a new virtual environment in Python
Open your command prompt or terminal.
Navigate to the directory where you want to create the virtual environment.
Run the following command to create a new virtual environment
python -m venv myenv//Replace "myenv" with the name you want to give for your virtual environment.python -m venv venv
Activate the virtual environment by running the activate script in the Scripts directory:
source myenv/bin/activate (Linux/Mac)myenv\Scripts\activate (Windows)
To exit the virtual environment, you can run the command:
deactivate
fastAPI
// 开发环境启动fastapi dev main.py// 生产环境启动uvicorn main:app --host 0.0.0.0 --port 80main: the file main.py (the Python "module").app: the object created inside of main.py with the line app = FastAPI().// 生产环境启动 in "daemon" mode (i.e., as a background process)nohup uvicorn main:app --host 0.0.0.0 --port 9000 > /dev/null 2>&1 &
Poetry
Poetry 是一个用于管理 Python 项目的依赖和构建过程的工具。它简化了包管理和虚拟环境的创建,让开发者更容易管理项目的依赖关系。
Install Poetry Using Official Script
curl -sSL https://install.python-poetry.org | python3 -
You can test that everything is set up by executing
poetry --version
Add Poetry to PATH
如果是上述脚本安装的,通常不用这一步,会自动加到path里 If Poetry is not found after installation, add it to your PATH manually:
export PATH="$HOME/.local/bin:$PATH"
To make this change permanent, add it to your shell configuration file:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrcsource ~/.bashrc// Use ~/.zshrc instead of ~/.bashrc if you're using zsh