VS Code安装aiogram后仍报ModuleNotFoundError的求助
解决VS Code中aiogram模块找不到的问题
一、先解决ModuleNotFoundError: No module named 'aiogram'问题
1. 确认Python解释器一致性
- 查看VS Code左下角显示的当前Python解释器版本(比如
Python 3.10.12 64-bit) - 打开终端,执行命令查看pip关联的Python路径:
- Windows:
where python和where pip - Linux/macOS:
which python和which pip
- Windows:
- 如果终端显示的路径和VS Code中的解释器不匹配,点击VS Code左下角的解释器选项,选择与pip对应的Python版本
2. 用指定Python版本安装
直接通过VS Code选中的Python版本执行安装命令,避免版本混淆:
- Windows:
py -3.x -m pip install aiogram(将3.x替换为你的实际Python版本,比如3.10) - Linux/macOS:
python3.x -m pip install aiogram - 若使用虚拟环境,先激活环境再安装:
- Windows:
venv\Scripts\activate - Linux/macOS:
source venv/bin/activate
激活后执行pip install aiogram
- Windows:
3. 强制重新安装
若安装过程出现文件损坏,执行以下命令重装:
pip uninstall -y aiogram pip install aiogram --force-reinstall
二、代码兼容问题(模块找到后可能遇到的报错)
你的代码是aiogram 2.x的写法,如果安装的是最新的aiogram 3.x,会出现语法错误,可选择以下两种方案:
方案1:安装aiogram 2.x兼容版本
执行命令安装指定版本,直接适配现有代码:
pip install aiogram==2.25.1
方案2:升级代码适配aiogram 3.x
修改后的代码示例:
import logging from aiogram import Bot, Dispatcher, types from aiogram.filters import Command from aiogram.types import Message import requests TELEGRAM_BOT_TOKEN = 'XXXXXXXX' COINGECKO_API_KEY = 'XXXXXXXX' logging.basicConfig(level=logging.INFO) bot = Bot(token=TELEGRAM_BOT_TOKEN) dp = Dispatcher() # Function to get token price from CoinGecko def get_token_price(token_id): url = f'https://api.coingecko.com/api/v3/simple/price?ids={token_id}&vs_currencies=usd' headers = {'Authorization': f'Bearer {COINGECKO_API_KEY}'} response = requests.get(url, headers=headers) data = response.json() if 'error' in data: logging.error(f'Error from CoinGecko API: {data["error"]}') return None try: price = data[token_id]['usd'] return price except KeyError: logging.error(f'KeyError: Could not find price for token with ID {token_id}') return None # Command handlers @dp.message(Command('btc')) async def get_btc(message: Message): price = get_token_price('bitcoin') if price is not None: await message.reply(f'Ціна Bitcoin: {price} USD') else: await message.reply('Не вдалося отримати ціну для Bitcoin') @dp.message(Command('eth')) async def get_eth(message: Message): price = get_token_price('ethereum') if price is not None: await message.reply(f'Ціна Ethereum: {price} USD') else: await message.reply('Не вдалося отримати ціну для Ethereum') @dp.message(Command('sfund')) async def get_sfund(message: Message): price = get_token_price('seedify-fund') if price is not None: await message.reply(f'Ціна Seedify: {price} USD') else: await message.reply('Не вдалося отримати ціну для Seedify Fund') @dp.message(Command('cgpt')) async def get_cgpt(message: Message): price = get_token_price('chaingpt') if price is not None: await message.reply(f'Ціна ChainGPT: {price} USD') else: await message.reply('Не вдалося отримати ціну для ChainGPT') @dp.message(Command('bnb')) async def get_bnb(message: Message): price = get_token_price('binancecoin') if price is not None: await message.reply(f'Ціна BNB: {price} USD') else: await message.reply('Не вдалося отримати ціну для Binance Coin (BNB)') if __name__ == '__main__': import asyncio asyncio.run(dp.start_polling(bot, skip_updates=True))
内容的提问来源于stack exchange,提问作者Slay
相关产品推荐
相关产品推荐

