Some Cool Python Tricks

Words
243
Reading
2 min
8y
Listen to this post
~2 min

Some tricks to help make upgrading a Python application, I've been using some of these tricks
and tools when working in Django projects.

  • First trick is to create a virtualenv for Python3.6:
$ virtualenv foo-env -p python3.6
$ source foo-env/bin/activate
(foo-env) $ cd foo/src/app/
(foo-env) $ pip3 install -r requirements.txt

Here is a wiki entry on my wiki on the topic:
https://yoirtuts.com/index.php?title=Create_Virtual_Env_And_Activate_Python3

  • How to delete all the dependencies in a virtualenv and reinstall the dependencies
    from a new requirements.txt file.

This command creates a list of dependencies that are installed, we are gonna pass this list to the pip3 uninstall command.
As you can see below this example is done in a virtualenv with the virtualenv activated.

venv) $ pip3 freeze > uninstall-requirements.txt

Now we uninstall the list of currently installed software that we saved to the text file "uninstall-requirements.txt"

venv) $ pip3 uninstall -r uninstall-requirements.txt -y

Now see if everything uninstalled:(You shouldn't see anything more than this list below)

venv) $ pip3 list
Package    Version
---------- -------
pip        10.0.1
setuptools 39.0.1
wheel      0.31.0

Now install your requirements with:

venv) $  pip3 install -r requirements.txt

That piece of information I found on here:

https://stackoverflow.com/questions/11248073/what-is-the-easiest-way-to-remove-all-packages-installed-by-pip/40566052#40566052

Now for some more cool tips that I use often.

Run a Python script with even more verbose output:

$ python3 -Wd script.py

or even worse:


$ python3 -W once script.py

Find Dependencies top level and others:

Sometimes I find this very useful, especially when working on
large old school django web applications:


venv) $ pip3 install pipdeptree

Then scroll over the output

venv) $ pipdeptree | less

This is useful sometimes when you run into issues such as this one:

...
botocore 1.0.0 has requirement jmespath==0.7.1, but you'll have jmespath 0.9.3 which is incompatible.
...

You will usually get these kind of errors if you run , in the case where I got
this error it happened that the application was updated to discourage people from using a package with dependencies on old packages.

venv) $ pip3 install -r requirements.txt 

Some Cool Python Tricks | Ecency