gpt4 book ai didi

python - psycopg2 上的 ModuleNotFoundError 仅在编译脚本上

转载 作者:行者123 更新时间:2023-11-28 19:02:35 25 4
gpt4 key购买 nike

我目前正在学习使用 psycopg2 进行数据库输入的教程,但我似乎无法让我的脚本找到它。使用 pip install,我的脚本是用 Atom 编写的。当我在命令提示符下导入 psycopg2 时它工作正常但我的 Atom 脚本不会运行:

原子脚本

import sys
sys.path.append('c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages')
print(sys.path)
import psycopg2

def create_table():
conn=psycopg2.connect("dbname='database1' user='postgres' password='postgres123' host='localhost' port='5432'")
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()

def insert(item,quantity,price):
conn=psycopg2.connect("lite.db")
cur=conn.cursor()
cur.execute("INSERT INTO store VALUES (?,?,?)",(item,quantity,price))
conn.commit()
conn.close()

def view():
conn=psycopg2.connect("lite.db")
cur=conn.cursor()
cur.execute("SELECT * FROM store")
rows=cur.fetchall()
conn.close()
return rows

def delete(item):
conn=psycopg2.connect("lite.db")
cur=conn.cursor()
cur.execute("DELETE FROM store WHERE item=?",(item,))
conn.commit()
conn.close()

def update(quantity,price,item):
conn=psycopg2.connect("lite.db")
cur=conn.cursor()
cur.execute("UPDATE store SET quantity=?, price=? WHERE item=?",(quantity,price,item,))
conn.commit()
conn.close()

create_table()
#print(view())

命令输出

C:\Users\J.Meiring\Documents\Py\Database>script2.py
['C:\\Users\\J.Meiring\\Documents\\Py\\Database', 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\python36.zip', 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\DLLs', 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib', 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64', 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages', 'c:\\users\\j.meiring\x07ppdata\\local\\programs\\python\\python36-32\\lib\\site-packages']
Traceback (most recent call last):
File "C:\Users\J.Meiring\Documents\Py\Database\script2.py", line 4, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

C:\Users\J.Meiring\Documents\Py\Database>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print("'c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages' in sys.path")
'c:\users\j.meiringppdata\local\programs\python\python36-32\lib\site-packages' in sys.path
>>> print('c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages' in sys.path)
False
>>> sys.path.append('c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages')
>>> print('c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages' in sys.path)
True
>>> import psycopg2
>>>

最佳答案

首先,您在代码中显示了这一点,将路径添加到 sys.path:

sys.path.append('c:\\users\j.meiring\appdata\local\programs\python\python36-32\lib\site-packages')

但是,您的错误看起来有所不同。为了便于阅读,我在它提供的路径中添加了换行符。

['C:\\Users\\J.Meiring\\Documents\\Py\\Database',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\python36.zip',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\DLLs',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages',
'c:\\users\\j.meiring\x07ppdata\\local\\programs\\python\\python36-32\\lib\\site-packages']

在最后一行注意这部分路径...

'c:\\users\\j.meiring\x07ppdata\\local......

在这里,您看到的不是 appdata,而是 x07ppdata。这可能是 0x07,或 BELL 字符(控制字符,通常不可见)。可能这个字符是嵌入在您的文件中的某种 unicode 而您没有注意到,因为它在呈现时看起来是正确的?在任何情况下,这个 x07ppdata 目录都不存在,因此该路径没有帮助。

关于python - psycopg2 上的 ModuleNotFoundError 仅在编译脚本上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com