Useful shell commands for Python

Table of content:

  • PIP
    • Install module
    • Uninstall module
    • Run module
    • List
    • Check outdated
    • Check version
    • Freez
    • Requirements
  • Virtual environment
    • Create
    • Enable
    • Disable
    • Delete
  • TBC

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
  • python3" tells which python version should be used. For example in can be more specific like "python3.11"
  • "-m ” part is used to run a Python module as a script. It allows you to execute built-in or third-party Python modules directly from the command line. In this case it is „venv” module
  • „venv” like said befor, name of the module to be run
  • „.venv” this is a name for new virtual environement. Directory with the same name will be created so use . (dot) at the begin to create hidden directory

Activating virtual environement

source .venv/bin/activate
  • „source” is not a built-in Python command—it is a shell command commonly used in Unix-like systems to activate a virtual environment or execute a script within the current shell environment.
  • second part is a path to the activation script, which in python virtual environment is located inside „bin” directory.
  • „.venv” is a name of virtual environment, dot at the begin means it is in hidden directory

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

Hello 👋 Nice to meet you.

Sign up here if you would like to be notified when new content is added to the blog.

We will not spam you. You will only receive important information, such as changes to the blog or new content.

Dodaj komentarz