Anki

How can you check all the pip packages installed in the current Python environment?   python pip

pip list.

How can you check the latest version of pip packages?   python pip

pip list -o.

Describe what __name__ = __main__ does in Python.   python

Suppose we have the following 2 .py files:

# module_1.py

print(f"Name of first module is {__name__}\n")

def some_function():
    print(f"Output of function in module {__name__}\n") 

if __name__ == __main__:
    some_function()
# module_2.py
import module_1

print(f"Name of second module is {__name__}\n")

Running python module_1.py will give:

Name of the first module is __main__
Output of function in module __main__

Running python module_2.py will give:

Name of the first module is module_1
Name of the second module is __main__

Key ideas:

  • The __name__ variable of the Python file being executed is always __main__.
  • Importing module_1 in module_2.py executes the code in module_1.py.
  • Having the checking for __name__ == __main__ is a good way to let others know that the file is meant to be executed, unlike a file that may only have class and function definitions.

This example is from this video.

TODO Make the code blocks run by making them share the same runtime.

What is the difference between conda, pip, virtualenv, and venv?   python

  • pip is a package manager for Python, used to install and manage Python packages.
  • virtualenv, venv are Python environment managers, often used to isolate dependencies per project. For e.g. two projects may require 2 versions of the same Python package - having a separate Python environment for each project enables the installation of 2 versions in different environments.
  • conda handles both package management and environment management together. It is also not restricted to just Python packages.

Emacs 29.4 (Org mode 9.6.15)