Importing a library from (or near) a script with the same name raises “AttributeError: module has no attribute” or an ImportError or NameError

This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name. An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name … Read more

How to move all modules to new version of Python (from 3.6 to 3.7)

Even if the old python version has been removed, it is possible to use the pip of the current python version with the –path option to list all the modules installed in the previous version. For example, migrating all my user installed python modules from 3.7 to 3.8 pip freeze –path ~/.local/lib/python3.7/site-packages > requirements.txt pip … Read more

How do I extend a python module? Adding new functionality to the `python-twitter` package

A few ways. The easy way: Don’t extend the module, extend the classes. exttwitter.py import twitter class Api(twitter.Api): pass # override/add any functions here. Downside : Every class in twitter must be in exttwitter.py, even if it’s just a stub (as above) A harder (possibly un-pythonic) way: Import * from python-twitter into a module that … Read more