Reset to default
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
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 of your script in question is matching the module you are trying to import:
Notice the name you used in your script:
File "/Users/me/dev/rough/requests.py", line 1, in <module>
The module you are trying to import:
Rename your module to something else to avoid the name collision.
Python may generate a
requests.pyc file next to your
requests.py file (in the
__pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the
pyc file in
__pycache__ should not affect your code if the
py file has been removed.
In the example, renaming the file to
requests.pyc, and running again successfully prints
The error occurs because a user-created script has a name-clash with a library filename. Note, however, that the problem can be caused indirectly. It might take a little detective work to figure out which file is causing the problem.
For example: suppose that you have a script
mydecimal.py that includes
import decimal, intending to use the standard library
decimal library for accurate floating-point calculations with decimal numbers. That doesn’t cause a problem, because there is no standard library
mydecimal. However, it so happens that
numbers (another standard library module) for internal use, so a script called
numbers.py in your project would cause the problem.
If you still encounter problems like this after tracking own and renaming or removing the appropriate
.py files in your project, also check for
.pyc files that Python uses to cache bytecode compilation when importing modules. In 3.x, these will be stored in folders with the special name
__pycache__; it is safe to delete such folders and files, and possible to suppress them (but you normally won’t want to).