Python notes
Learning Python
- Follow the table of contents on the right side on this page: https://python.land/introduction-to-python
Other resources
- https://www.python.org/about/
- 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
- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
- 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
-
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
- 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
-
To understand what each of these files do, see https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
-
A website may consist of one or more sections.
- For example, main site, blog, wiki, downloads area, catalog, etc.
- Django encourages you to develop these components as separate applications, which could then be re-used in different projects if desired.
-
Use
manage.py
to create one or more applications insidemy_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
- What is
__init__.py
in each application?- An empty file created in all applications so that
Django/Python
will recognize the folder as aPython Package
and allow you to use its objects within other parts of the project.
- An empty file created in all applications so that
- What is
-
Register the new
catalog
application to include them in the mainmy_python_application1
project.- 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 theINSTALLED_APPS
list in the project settings. - Go to
my_python_application1/my_python_application1/settings.py
, find the definition for theINSTALLED_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 ]
- Now that the
-
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
- Set up a simple django application
- Where to write the code?
- Put it in
__init.py__
- Put it in
- Where to write the tests?
- 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 placetest.py
above the package folder, so the directory tree will look something like this:project/ │ ├── my_sample/ │ └── __init__.py | └── test.py
- To get started writing tests, you can simply create a file called
IDEs
- PyCharm from Jetbrains
Packaging and Dependency Management
- Poetry: https://python-poetry.org/