我目前正在使用psycopg2进行有关数据库输入的教程,但似乎无法获取脚本来查找它。使用过的pip安装,我的脚本是用Atom编写的。当我在命令提示符下导入psycopg2时,它工作正常,但我的Atom脚本无法运行:
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())
CMD输出
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
>>>
答案 0 :(得分:0)
首先,您需要在代码中显示此内容,并向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
目录不存在,因此该路径无济于事。