Importing installed package from 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

What’s the difference between __builtin__ and __builtins__?

Straight from the python documentation: http://docs.python.org/reference/executionmodel.html By default, when in the __main__ module, __builtins__ is the built-in module __builtin__ (note: no ‘s’); when in any other module, __builtins__ is an alias for the dictionary of the __builtin__ module itself. __builtins__ can be set to a user-created dictionary to create a weak form of restricted execution. … Read more

What exactly is a “raw string regex” and how can you use it?

Zarkonnen’s response does answer your question, but not directly. Let me try to be more direct, and see if I can grab the bounty from Zarkonnen. You will perhaps find this easier to understand if you stop using the terms “raw string regex” and “raw string patterns”. These terms conflate two separate concepts: the representations … Read more

Can modules have properties the same way that objects can?

As PEP 562 has been implemented in Python >= 3.7, now we can do this file: module.py def __getattr__(name): if name == ‘y’: return 3 raise AttributeError(f”module ‘{__name__}’ has no attribute ‘{name}'”) other = 4 demo: >>> import module >>> module.y 3 >>> module.other 4 >>> module.nosuch Traceback (most recent call last): File “<stdin>”, line … Read more

Installing python module within code

The officially recommended way to install packages from a script is by calling pip’s command-line interface via a subprocess. Most other answers presented here are not supported by pip. Furthermore since pip v10, all code has been moved to pip._internal precisely in order to make it clear to users that programmatic use of pip is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)