Herramientas de usuario

Herramientas del sitio


wiki2:python:notes

¡Esta es una revisión vieja del documento!


Python notes

Time zones

The best practice is to store the datetime in the DB as UTC. To use time-zone-aware datetime objects internally, and to translate them to the end user’s time zone. It avoids the problem with Daylight Saving Time (DST), the zones where the clock is moved in spring and autumn.

Libraries to work with time zones and UTC are pytz and arrow.

Python’s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information, represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset, a datetime object is aware. Otherwise, it’s naive.

You can use is_aware() and is_naive() to determine whether datetimes are aware or naive.

PYTHONPATH

Python does not add the current directory to sys.path, but rather the directory that the script is in. Add the project directory to either sys.path or $PYTHONPATH

$ export PYTHONPATH=$(pwd);python resources/manage_server.py

Pip tools

Pip

If you wanted to install a module using pip3:

python3 -m pip install --upgrade pip
python3 -m pip install jupyter

Behind a proxy

sudo pip --proxy http://web-proxy.mydomain.com install somepackage

Split requirements

You can have this in one base file:

# Django
Django==1.8.9
django-configurations==1.0
...

And this in another:

-r base.txt

# Mocking
mock==1.3.0
requests-mock==1.3.0
...

Pipenv

https://pipenv.readthedocs.io

pipenv install create the required pipfiles.

pipenv shell activate the virtualenv.

pipenv run python manage.py runserver runs python manage.py runserver inside the virtualenv.

pipenv –rm removes the virtual env.

pipenv –python 3.6 tell pipenv to use python 3.6

You can specify where to place the virtual environment with the environment variable WORKON_HOME:

export WORKON_HOME=~/.venvs

To start a project using 3.7:

pipenv install --python 3.7
wiki2/python/notes.1545389552.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)