求助:导入Session模块失败及coinmarketcap导入报错问题
Alright, let's tackle these two Python import errors you're facing—they're both pretty common issues once you know where to look. Here's how to fix each one:
1. ImportError: cannot import name 'Session' with requests
This error usually boils down to one of three easy-to-fix problems:
- You have a local file named
requests.py
If you accidentally created a file calledrequests.pyin your project folder, it'll override the officialrequestslibrary when you try to import. Rename that file right away (and delete anyrequests.pycfile that might have popped up too—those are cached bytecode files). - Your requests version is way too old
TheSessionclass has been part of requests for ages, but if you're running a super outdated version (pre-2.0.0), it might not exist. Check your current version with:
If it's old, update it using:pip show requestspip install --upgrade requests - Your requests installation is corrupted
Sometimes files get messed up during installation. Uninstall and reinstall the library to fix this:pip uninstall requests -y pip install requests
2. Import error with the coinmarketcap library
Looking at your traceback, the error comes from the library's core.py file. Here's what's likely going on:
- Python 3.6 is no longer supported
You're using Python 3.6, which reached end-of-life years ago. The oldercoinmarketcappackage on PyPI hasn't been updated in a long time and probably doesn't work with 3.6 anymore. If you can, upgrade to a supported Python version (3.8 or newer is recommended). - You're using an unmaintained library
The originalcoinmarketcapPyPI package is abandoned. Instead, use the official CoinMarketCap API wrapper calledpython-coinmarketcap. First, remove the old package:
Then install the official one:pip uninstall coinmarketcap -y
Adjust your import code to match the new wrapper:pip install python-coinmarketcapfrom coinmarketcap import CoinMarketCap # You'll need an API key from CoinMarketCap's website cmc = CoinMarketCap('your_api_key') - Missing or incompatible dependencies
If you really need to stick with the old library, check itsrequirements.txtfile (you can find it on the package's GitHub repo) and install exact versions of dependencies that work with Python 3.6. For example, it might require an older requests version likerequests==2.20.0.
内容的提问来源于stack exchange,提问作者hack-is-art
相关产品推荐
相关产品推荐

