• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Python | Installing Python
  1. References
  2. Python Installation (3.12.3)
  3. Creating a virtual environment
  4. Activating a virtual environment
  5. Deactivating a virtual environment
  6. Package Management
  7. The Zen of Python, by Tim Peters

  1. References
    The official home of the Python Programming Language:
    https://python.org

    Python Developer’s Guide:
    https://devguide.python.org

    Visual Studio Code: source-code editor, by Microsoft:
    https://code.visualstudio.com

    PyCharm: the Python IDE, by JetBrains:
    https://www.jetbrains.com/pycharm

    Jupyter:
    https://jupyter.org
  2. Python Installation
    You can use your os package manger to install Python and any other optional packages.

    Ubuntu:
    $ sudo apt install python3 python3-dev python3-pip python3-venv
    Red Hat:
    $ sudo dnf install python3 python3-devel python3-pip python3-venv
    You can also install Python from source: https://devguide.python.org/getting-started/setup-building/

    Check your installation:
    $ python3 --version
    Python 3.12.3
    $ pip --version
    pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
    Run a simple script from the command line:
    $ python3 -c "print('hello python')"
    hello python
    Create and run a simple script:
    $ echo "print('Hello Python')" > hello.py
    $ python3 hello.py
    Hello Python
    Accessing Python Shell:
    $ python3
    Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print('Hello Python')
    Hello Python
    >>> quit()
  3. Creating a virtual environment
    A virtual environment is an environment where all new installed packages are isolated and independent from the Python base installation. Within the virtual environment you can use packages that have already been installed in the Python base installation.

    To create a virtual environment, you need run the "venv" virtual environment module from a specific location of your project. You need to provide a location and name for the virtual environment.

    See this page for more details about the venv module: https://docs.python.org/3/library/venv.html

    Create project directory:
    $ mkdir -p ~/dev/python/my_project
    $ cd ~/dev/python/my_project
    Create virtual environment:
    $ python3 -m venv .venv
    Directory structure:
    $ tree -L 1 .venv
    .venv/
    ├── bin
    ├── include
    ├── lib
    └── pyvenv.cfg
    Python project structure:
    my_project/
    ├── .venv/                 # virtual environment
    ├── src/
    │   └── my_package/
    │       ├── __init__.py
    │       └── main.py
    ├── tests/
    │   ├── __init__.py
    │   └── test_main.py
    ├── docs/
    ├── .gitignore            # exclude .venv, __pycache__, ...
    ├── README.md
    ├── requirements.txt      # production dependencies
    ├── requirements-dev.txt  # development dependencies
    ├── pytest.ini            # pytest configuration
    ├── setup.py              # package configuration
    └── pyproject.toml        # packaging tools configuration
  4. Activating a virtual environment
    To activate a virtual environment, you need to execute the command "activate" in ".venv/bin/":
    $ source .venv/bin/activate
    (.venv) $
    When the virtual environment is active, you will see the name of the environment in parentheses ("(.venv)").

    Environment verification:
    (.venv) $ which python
    ~/dev/python/my_project/.venv/bin/python
    (.venv) $ python3 --version
    Python 3.12.3
    (.venv) $ pip list
    Package Version
    ------- -------
    pip     24.0
  5. Deactivating a virtual environment
    To deactivate and stop using a virtual environment, you need to execute the command "deactivate":
    (.venv) $ deactivate
    $
    You can also deactivate the virtual environment by closing the terminal it's running in.

    The packages installed in the virtual environment will not be available when the environment is deactivated.

    To clean the virtual environment in your project directory, you can simply delete the virtual environment folder:
    $ rm -rf ~/dev/python/my_project/.venv/
  6. Package Management
    Install a package:
    $ python3 -m pip install package-name
    Use "--user" to install the package for the current user only:
    $ python3 -m pip install --user package-name
    Install a specific version of a package:
    $ python3 -m pip install package-name==package-version
    Install packages from requirements file:
    $ python3 -m pip install -r requirements.txt
    List installed packages:
    $ python3 -m pip list
    Show package information:
    $ python3 -m pip show package-name
    Upgrade a package:
    $ python3 -m pip install --upgrade package-name
    Upgrade pip itself:
    $ python3 -m pip install --upgrade pip
    Uninstall a package:
    $ python3 -m pip uninstall package-name
    Create a requirements file:
    $ python3 -m pip freeze > requirements.txt
  7. The Zen of Python, by Tim Peters
    Python's guiding principles:
    $ python
    >>> import this
    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    >>>
© 2025  mtitek