Here’s a comprehensive list of Python CLI commands and techniques, including specific tools and configurations for PyCharm, PyEnvy and Python 3.
-
Check Python version:
python --version python3 --version
-
Update Python (using
pyenvfor version management):pyenv install <version> # Install specific version pyenv global <version> # Set global Python version pyenv local <version> # Set local Python version
-
List installed Python versions (using
pyenv):pyenv versions
-
List available Python versions (using
pyenv):pyenv install --list
-
Run a Python script:
python script.py python3 script.py
-
Run a script with additional arguments:
python script.py arg1 arg2
-
Run Python in interactive mode:
python python3
-
Run Python with debugging:
python -m pdb script.py
-
Run a script with specific environment variables:
ENV_VAR=value python script.py
-
Install a package:
pip install package-name pip3 install package-name
-
Install a package from a
requirements.txtfile:pip install -r requirements.txt
-
Uninstall a package:
pip uninstall package-name
-
List installed packages:
pip list
-
Show details of a specific package:
pip show package-name
-
Update a package to the latest version:
pip install --upgrade package-name
-
Check for outdated packages:
pip list --outdated
-
Freeze the current environment’s packages to a file:
pip freeze > requirements.txt -
Check for vulnerabilities in installed packages (using
safety):pip install safety safety check
-
Create a virtual environment:
python -m venv venv python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate # Unix/macOS .\venv\Scripts\activate # Windows
-
Deactivate the virtual environment:
deactivate
-
Remove a virtual environment:
rm -rf venv # Unix/macOS rmdir /s /q venv # Windows
-
Run Python with optimizations:
python -O script.py
-
Compile Python scripts to bytecode:
python -m py_compile script.py
-
Compile Python scripts to an executable (using
PyInstaller):pip install pyinstaller pyinstaller --onefile script.py
-
Generate a profile of your Python code’s performance (using
cProfile):python -m cProfile script.py
-
Generate a code coverage report (using
coverage):pip install coverage coverage run script.py coverage report
-
Start debugging with
pdb(Python Debugger):python -m pdb script.py
-
Set a breakpoint in your code:
import pdb; pdb.set_trace()
-
Debug with
ipdb(IPython Debugger):pip install ipdb python -m ipdb script.py
-
Run tests with
unittest:python -m unittest discover
-
Run tests with
pytest:pip install pytest pytest
-
Run tests with
nose:pip install nose nosetests
-
Lint Python code with
flake8:pip install flake8 flake8 script.py
-
Lint Python code with
pylint:pip install pylint pylint script.py
-
Format Python code with
black:pip install black black script.py
-
Check code formatting with
pylint:pylint --max-line-length=88 script.py
-
Create a new package template:
python setup.py sdist
-
Build a package:
python setup.py sdist bdist_wheel
-
Upload a package to PyPI:
pip install twine twine upload dist/* -
Download a package from PyPI:
pip download package-name
-
Set environment variables:
export ENV_VAR=value # Unix/macOS set ENV_VAR=value # Windows
-
Use
.envfiles withpython-dotenv:pip install python-dotenv
In
script.py:from dotenv import load_dotenv load_dotenv()
-
Run PyCharm from the command line:
pycharm # or pycharm64, depending on your installation -
Open a project in PyCharm from the command line:
pycharm /path/to/project
-
Open a file in PyCharm from the command line:
pycharm /path/to/file.py
-
Configure a Python interpreter in PyCharm:
- Go to
File > Settings > Project > Python Interpreter - Add or select the interpreter (Python 2.x or 3.x)
- Go to
-
Use PyCharm's Terminal to execute commands:
- Open the Terminal tab within PyCharm to run Python commands,
pipcommands, and other shell commands.
- Open the Terminal tab within PyCharm to run Python commands,
-
Run configurations and debugging in PyCharm:
- Create and manage run/debug configurations through
Run > Edit Configurations. - Use breakpoints and the integrated debugger to step through code.
- Create and manage run/debug configurations through
-
Manage Python environments in PyCharm:
- Create and manage virtual environments via
File > Settings > Project > Python Interpreter > Add.
- Create and manage virtual environments via
-
Install a package in PyCharm’s Terminal:
pip install package-name
-
Check package versions and dependencies within PyCharm:
- Use the "Project Interpreter" settings to view and manage packages.
This extensive list covers fundamental and advanced Python CLI commands, including package management, virtual environments, debugging, testing, and code quality. It also includes PyCharm-specific commands and features for effective development and environment management within PyCharm.