Site icon ucdev

Useful shell commands for Python

Table of content:

Install module

python3 -m pip install package_name

or

pip install package_name

Uninstall module

pip uninstall package_name

Run module

python3 script_name.py arg1 arg2 ...

List of modules

pip list

List of outdated modules

pip list --outdated

Version check

python3 package_name --version

Freeze

This is a very useful command that creates a single text file with a list of all installed modules with information about the exact version. It can be used to quickly restore the same Python environment, with a single command

pip freeze > requirements.txt

Requirements file

If you create a requirements.txt file with pip freeze, the following command will help you install all the packages in the file with a single command

pip install -r <path_to_requirements_file>

Creating virtual environment

python3 -m venv .venv

Activating virtual environement

source .venv/bin/activate

Checking active virtual environement

echo $VIRTUAL_ENV

To check which virtual environment is active, you can print the environment variable „VIRTUAL_ENV” to see the full path to the active virtual environment. Normally, when a virtual environment is active, you can only see its name as a prefix to the shell prompt.

Deactivating virtual environement

deactivate

Deleting virtual environement

rm -rf .venv

Deleting a Python virtual environment is easy because a virtual environment is just a directory that contains certain files and folders. You can delete it the same way you would delete any directory. In this example, we will run the remove command „rm” to delete a virtual environment named „.venv” from the same location.

Check Syntax Errors in a Script

python -m py_compile script.py

Run Python Debugger (PDB)

python -m pdb script.py

Find the Location of the Python Interpreter

which python

Exit mobile version