简体   繁体   English

初学者:Python 找不到 'pyodbc' 包?

[英]Beginner: Python can't find 'pyodbc' package?

I am very new to the Python language and have a small program.我对 Python 语言很陌生,有一个小程序。 It had been working but something change and now I can't get it to run.它一直在工作,但有些变化,现在我无法让它运行。 It's having a problem with finding 'pyodbc'.找到'pyodbc'有问题。 I installed the 'pyodbc' package so I don't understand why there error.我安装了“pyodbc”包,所以我不明白为什么会出现错误。 I am using Python 3.7.6.我正在使用 Python 3.7.6。 Thank you for your help!感谢您的帮助!

pip install pyodbc pip 安装 pyodbc

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Requirement already satisfied: pyodbc in c:\users\c113850\appdata\roaming\python\python37\site-packages (4.0.28)

Code:代码:

import requests
import pyodbc
from bs4 import BeautifulSoup
from datetime import datetime
import pytz 
import time
import azure.functions

page = requests.get("https://samplepage.html")

if page.status_code == 200:
    print(page.status_code)
    #print(page.content)

    soup = BeautifulSoup(page.content, 'html.parser')
    print(soup.title)
    rows = soup.find_all('tr')
    # for row in rows:          # Print all occurrences
    #    print(row.get_text())
    print(rows[0])
    print(rows[7])
    pjmtime = rows[0].td.get_text()
    print("PJM = ",pjmtime)

    #dt_string = "Tue Jan 21 18:00:00 EST 2020"
    dt_object = datetime.strptime(pjmtime, "%a %b %d %H:%M:%S EST %Y")
    print("Timestamp =", dt_object)

    eastern=pytz.timezone('US/Eastern')
    date_eastern=eastern.localize(dt_object,is_dst=None)
    date_utc=date_eastern.astimezone(pytz.utc)
    print("UTC =", date_utc)

    row = soup.find(text='PRICE').parent.parent
    name = row.select('td')[0].get_text()
    typed = row.select('td')[1].get_text()
    weighted = row.select('td')[2].get_text()
    hourly = row.select('td')[3].get_text()

    server = 'db.database.windows.net'
    database = '...'
    username = '...'
    password = '...'
    driver = '{ODBC Driver 17 for SQL Server}'

    cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()
    print("insert into [PJMLocationalMarginalPrice] ([Name],[Type],[WeightedAverage],[HourlyIntegrated],[TimeStamp]) values(?,?,?,?,?)",
               (name,typed,weighted,hourly,date_utc))
    cursor.execute("insert into [PJMLocationalMarginalPrice] ([Name],[Type],[WeightedAverage],[HourlyIntegrated],[TimeStamp]) values (?,?,?,?,?)",
               (name,typed,weighted,hourly,date_utc))
    cnxn.commit()
else:
    print("Error: page not open")

Error:错误:

Traceback (most recent call last):
  File "c:/Users/C113850/PycharmProjects/Scraping101/Scraping.py", line 2, in <module>
    import pyodbc
ImportError: DLL load failed: The specified module could not be found.

Update: I was looking at the folders under site-packages and noticed that 'pyodbc' folder is not there but 'pyodbc-4.0.28.dist-info' folder is there.更新:我正在查看 site-packages 下的文件夹,注意到“pyodbc”文件夹不在那里,但“pyodbc-4.0.28.dist-info”文件夹在那里。 在此处输入图片说明

The module was not properly installed.模块安装不正确。

Try reinstalling it:尝试重新安装它:

pip uninstall pyodbc
pip install pyodbc

If that doesn't work, try using pip3:如果这不起作用,请尝试使用 pip3:

pip uninstall pyodbc
pip3 install pyodbc

Did you have an active Python Environment when you installed 'pyodbc'.安装“pyodbc”时是否有活动的 Python 环境。 If so, you will want to activate the environment prior to running your script as you wont have access to the package outside that environment.如果是这样,您将需要在运行脚本之前激活环境,因为您无法访问该环境之外的包。

If not, you may just need to uninstall and re-install.如果没有,您可能只需要卸载并重新安装。

pip uninstall pyodbc
pip install pyodbc

I found something on Github that there is a bug in pyodbc version 4.0.28 so I downgraded to 4.0.27 and that resolved my issue.我在 Github 上发现 pyodbc 版本 4.0.28 中存在一个错误,因此我降级到 4.0.27 并解决了我的问题。 For more info, click here .如需更多信息,请单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM