During Python development, many commands and tool operations are easily forgotten. Whether you’re a beginner or an experienced developer, having a readily accessible “command cheatsheet” can significantly boost your efficiency.
This article summarizes commonly used commands for Python, pip, virtual environments, package management, file operations, debugging, formatting, performance testing, and code checking, serving as a comprehensive and essential memo for Python development.
You can bookmark this article or turn it into a single-page portable document.
1. Python Command Quick Reference
Basic Commands
| Purpose | Command |
|---|---|
| Check Python version | python --version / python3 --version |
| Enter interactive interpreter | python |
| Execute script | python script.py |
| Execute one line of code | python -c "print('hello')" |
| Run as module | python -m http.server 8000 |
2. pip Package Management Commands
Basic Operations
| Action | Command |
|---|---|
| Check pip version | pip --version |
| Install package | pip install package_name |
| Install specific version | pip install package==1.2.3 |
| Upgrade package | pip install --upgrade package |
| Uninstall package | pip uninstall package |
| List installed packages | pip list |
| List outdated packages | pip list --outdated |
Export/Install Dependency List
| Function | Command |
|---|---|
| Export dependencies | pip freeze > requirements.txt |
| Install dependencies | pip install -r requirements.txt |
Domestic Mirrors (Speed Boost)
| Mirror Source | Example |
|---|---|
| Tsinghua University | pip install package -i https://pypi.tuna.tsinghua.edu.cn/simple |
3. Virtual Environment Commands (venv)
Create Virtual Environment
bash
python -m venv venv
Activate Virtual Environment
| System | Command |
|---|---|
| Windows | venv\Scripts\activate |
| macOS/Linux | source venv/bin/activate |
Deactivate Virtual Environment
bash
deactivate
4. Poetry Commands (Modern Dependency Management)
Initialize Project
bash
poetry init
Install All Dependencies
bash
poetry install
Install a Dependency Package
bash
poetry add requests
Enter Virtual Environment Shell
bash
poetry shell
5. Common Python Module Commands
(1) json Module
python
import json
json.dumps({"a": 1}, ensure_ascii=False) # dict → JSON
json.loads('{"a": 1}') # JSON → dict
(2) os Module (File & System Operations)
| Function | Command |
|---|---|
| Current directory | os.getcwd() |
| List files | os.listdir(path) |
| Check if file/directory | os.path.isfile() / os.path.isdir() |
| Create directory | os.makedirs(path, exist_ok=True) |
| Delete file | os.remove(file_path) |
(3) shutil (File Copy & Move)
python
shutil.copy(src, dst) shutil.move(src, dst) shutil.rmtree(folder)
6. requests Common Syntax Quick Reference
python
import requests
# GET
requests.get(url)
# POST
requests.post(url, data={"a": 1})
# JSON
requests.post(url, json={"key": "value"})
# Add headers
requests.get(url, headers={"User-Agent": "Mozilla"})
# Download file
requests.get(url).content
7. BeautifulSoup Common Commands
python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "lxml")
soup.title.text
soup.find("div")
soup.find_all("a")
soup.select("div.classname")
8. Pandas Common Commands
Read Files
python
pd.read_csv("a.csv")
pd.read_excel("a.xlsx")
Common Operations
python
df.head()
df.info()
df.describe()
df.to_excel("out.xlsx", index=False)
9. Numpy Common Commands
python
np.array([...]) np.zeros((3,3)) np.ones((2,4)) np.random.rand(5)
10. Debugging (pdb)
python
import pdb; pdb.set_trace()
Common Debugging Commands:
| Command | Meaning |
|---|---|
| n | Next line |
| c | Continue execution |
| p variable | Print variable |
| q | Quit debugging |
11. Logging Common Configuration
python
import logging
logging.basicConfig(level=logging.INFO)
logging.info("info msg")
logging.error("error msg")
12. Performance Testing Commands (timeit)
In Command Line:
bash
python -m timeit "x=5; x*x"
In Code:
python
import timeit
timeit.timeit("x=5; x*x")
13. Code Quality Checking
flake8 (Static Analysis)
bash
flake8 your_code.py
black (Code Formatter)
bash
black .
14. Packaging & Distribution
PyInstaller Packaging
bash
pyinstaller -F script.py
Build pip Package
bash
python setup.py sdist bdist_wheel twine upload dist/*
15. Other Useful Commands Collection
Check Module Installation Location
bash
python -m site
Find Module Path
python
import module; print(module.__file__)
List All .py Files in Current Directory
bash
ls *.py
Conclusion
This cheatsheet covers the most commonly used commands, tools, and library operations in Python practice, spanning multiple areas: environment setup, package management, routine coding, performance debugging, formatting, packaging, and more.
Whether you’re writing scripts, performing data analysis, building web scrapers, developing backend APIs, or creating automation tools, these commands can save you a tremendous amount of time.