Python notes

Learning Python

  1. Follow the table of contents on the right side on this page: https://python.land/introduction-to-python

Other resources

  1. https://www.python.org/about/
    1. https://wiki.python.org/moin/BeginnersGuide/Programmers
    2. https://python.swaroopch.com/
  2. Best practices for structuring code: https://docs.python-guide.org/writing/structure/

Installation

How to check if python is installed on your computer?

python3

The output will indicate whether it is already installed or not.

How to check if django is installed on your computer?

$ python -m django --version
/usr/bin/python: No module named django
pacman -S python-pip
pacman -S python-django

or

sudo apt-get install python3-pip

For other systems, https://docs.djangoproject.com/en/5.1/topics/install/

python -m django --version
4.2.11

Project structure

General tips about project structure

  1. https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
  2. https://python.land/project-structure

django

What is django?

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django

Creating a project using django-admin

  1. Use the django-admin tool to generate a project folder, the basic file templates, and manage.py, which serves as your project management script.

    django-admin startproject my_python_application1
    cd my_python_application
    
    1. Output:
    [explorer436@explorer436-p50-20eqs27p03 python-playground]$ tree .
     .
     └── my_python_application1
         ├── manage.py
         └── my_python_application1
             ├── asgi.py
             ├── __init__.py
             ├── settings.py
             ├── urls.py
             └── wsgi.py
    
    1. To understand what each of these files do, see https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website

    2. A website may consist of one or more sections.

      1. For example, main site, blog, wiki, downloads area, catalog, etc.
      2. Django encourages you to develop these components as separate applications, which could then be re-used in different projects if desired.
    3. Use manage.py to create one or more applications inside my_python_application1

      python3 manage.py startapp catalog
      

      The result file structure should look something like this:

      [explorer436@explorer436-p50-20eqs27p03 my_python_application1]$ find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
      .
      |-manage.py
      |-my_python_application1
      | |-urls.py
      | |-asgi.py
      | |-__init__.py
      | |-wsgi.py
      | |-settings.py
      | |-__pycache__
      | | |-settings.cpython-313.pyc
      | | |-__init__.cpython-313.pyc
      |-catalog
      | |-admin.py
      | |-migrations
      | | |-__init__.py
      | |-__init__.py
      | |-apps.py
      | |-views.py
      | |-models.py
      | |-tests.py
      
      1. What is __init__.py in each application?
        1. An empty file created in all applications so that Django/Python will recognize the folder as a Python Package and allow you to use its objects within other parts of the project.
    4. Register the new catalog application to include them in the main my_python_application1 project.

      1. Now that the catalog application has been created, we have to register it with the main project so that it will be included when any tools are run (like adding models to the database for example). Applications are registered by adding them to the INSTALLED_APPS list in the project settings.
      2. Go to my_python_application1/my_python_application1/settings.py, find the definition for the INSTALLED_APPS list, add this to that list.
        INSTALLED_APPS = [
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            # Add our new application
            'catalog.apps.CatalogConfig', # This object was created for us in /catalog/apps.py
        ]
        
  2. Hook up the url/path mapper for each application.

Use this project structure approach for micro services.

If you plan on including multiple smaller applications in a project, do not use this approach.

Reference: https://docs.python-guide.org/writing/structure/

Do not do this.

django-admin startproject samplesite

This will result in the tree

 [explorer436@explorer436-p50-20eqs27p03 python-playground]$ tree .
.
└── samplesite
    ├── manage.py
    └── samplesite
     ├── asgi.py
     ├── __init__.py
     ├── settings.py
     ├── urls.py
     └── wsgi.py

3 directories, 6 files

Repetitive paths are confusing for both your tools and your developers. Unnecessary nesting doesn’t help anybody (unless they’re nostalgic for monolithic SVN repos).

Do this instead.

django-admin startproject samplesite .

Resulting file tree

[explorer436@explorer436-p50-20eqs27p03 python-playground]$ tree .
.
├── manage.py
└── samplesite
    ├── asgi.py
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

2 directories, 6 files

Project set-up to write a function and test it

  1. Set up a simple django application
  2. Where to write the code?
    1. Put it in __init.py__
  3. Where to write the tests?
    1. To get started writing tests, you can simply create a file called test.py and put the test cases in that file. Because the file will need to be able to import your application to be able to test it, you want to place test.py above the package folder, so the directory tree will look something like this:
      project/
      │
      ├── my_sample/
      │   └── __init__.py
      |
      └── test.py
      

IDEs

  1. PyCharm from Jetbrains

Packaging and Dependency Management

  1. Poetry: https://python-poetry.org/

Testing

Python Testing


Links to this note