https://docs.python.org/3/tutorial/index.html
When starting a Python project, create a virtul environment to avoid various problems like conflicting global library versions. Activate the virtual environment.
https://docs.python.org/3/tutorial/venv.html
π $ mkdir my-python-project
π $ python3 -m venv ./my-python-project/venv
π $ cd my-python-project/
π $ ls venv/
bin include lib pyvenv.cfg
π $ source venv/bin/activate
(my-python-project) π $
(my-python-project) π $ deactivate
π $
Remember to add venv/
to .gitignore
.
(On Windows, activate the virtual environment with venv\Scripts\activate
.)
Manage packages with pip
.
(Browse packages in the default repository at https://pypi.org/.)
π $ source bin/activate
(my-python-project) π $ pip install -U Flask
Collecting Flask
Downloading flask-3.0.0-py3-none-any.whl.metadata (3.6 kB)
[β¦]
Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, Flask
Successfully installed Flask-3.0.0 Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-3.0.1 blinker-1.7.0 click-8.1.7 itsdangerous-2.1.2
(my-python-project) π $
A requirements.txt
file is helpful both to document the required packages our project uses and to install the requirements (i.e., if we or someone else starts working on the project in a new environment):
PySide2>=5.11.1
appdirs
This requirements.txt
file can be used by Pip to install the required packages:
π $ pip install -r requirements.txt
Variables are classified with a scope that Python sets when a variable is first assigned a value.
Python variable scopes are, from inner to outer: local to a function, within the enclosing function, global (module) scope, and finally built-in scope. These scope rules are sometimes described with the mnemonic “LEGB” (Local, Enclosing, Global, Built-In).
Python searches scopes from the inside/smallest/narrowest to the outside/largest/widest. Variables in narrower scopes will (by default) mask variables in wider scopes.
The global
and nonlocal
keywords can override the default scoping.
There’s no truly global scope β “global” variables are contained in the namespace of their module.
These scope rules apply only to variables (my_variable
) not object attributes (myObject.myAttribute
).
>>> import builtins
>>> dir(builtins)
There are also three other scopes: temporary loop variables in some comprehensions, exception reference variables in some try handlers, and local scopes in class statements.
Every Python file is its own module.
#
starts a comment, anywhere in a line.
PyDoc is the Python documentation tool. It reads docstrings in code and outputs documentation.
"""
This is the docstring introducing this module.
Details, detailsβ¦.
"""
def double(n):
"""This docstring summarizes the function's purpose and does NOT just reiterate the function signature. """
return 2 * n
print(double(2))
Python adds docstrings it finds to the __doc__
attribute of corresponding objects.
If we save the above code the file docstringsTest.py
, we can:
π $ python
>>> import docstringsTest
>>> print(docstringsTest.__doc__)
>>> print(docstringsTest.double.__doc__)
(See also PEP 257 for docstring conventions.)
There is a Style Guide for Python Code.
lowercase_words_separated_by_underscores
.ALL_CAPS
.CapitalizedWords
.Pylint is a popular linter that can be installed by Pip.
π $ pylint myfile.py