Python - 2.7和3.6中的语法无效,但在不同的地方 - 修订后的代码

时间:2018-05-23 13:08:08

标签: python python-3.x python-2.7 fedora psycopg2

下面的代码用于将数据插入到PostgreSQL数据库中 - 我已经注释掉了db代码(只是尝试打印SQL字符串)。但是,出于某种原因,我无法解决语法错误。

我使用Fedora 28 64位。

> python2.7 filename.py

> python3.6 filename.py

根据富有洞察力的评论,我现在遇到了psycopg2与2.7和3.6的问题。

2.7

[pol@polhost db]$ python2.7 ./ace_2_pg.py 
I am unable to connect to the database
Traceback (most recent call last):
  File "./ace_2_pg.py", line 12, in <module>
    cur = conn.cursor()
NameError: name 'conn' is not defined

3.6

[pol@polhost db]$ python3.6 ./ace_2_pg.py 
Traceback (most recent call last):
  File "./ace_2_pg.py", line 4, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
[pol@polhost db]$ 

我无法理解!

尝试安装psycopg2,但已经安装了。必须是,因为我之前已成功插入数据!

python2.7

[pol@polhost db]$ pip install psycopg2
Requirement already satisfied: psycopg2 in /usr/lib64/python2.7/site-packages

python3.6

[pol@polhost db]$ pip3 install psycopg2
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 7, in <module>
    from pip._internal import main
ImportError: No module named _internal

如果

,我将不胜感激

a)我可以为2.7和3.6以及

提供工作python

b)更多重要的是,接收一个解释我出错的地方!

我在最后包含输入文件(非常短)进行测试 - 输出SQLString就足够了。

============ python code ======================

from __future__ import print_function

import psycopg2

try:
  conn = psycopg2.connect("dbname='test' user='postgres' host='localhost'")
  print ('Connected!')
except:
  print ('I am unable to connect to the database')

cur = conn.cursor()

with open('/home/pol/Downloads/sware/acedb/ace_bis/src/dump/dump_2018-05-18_A_Jade.1.ace') as f:
  for line in f:
    words = line.split()

    print ('Line length is: ' + len(line))

    if "\\" in line: 
      continue

    if line[0:3] == " \\ ":
      print (line + 'beginning of data') 
      continue

    if (len(words) == 3):
      print (words[0], ' 1 ', words[1], '2', words[2], '3')
      SQLString = "INSERT INTO jade VALUES ('" + words[0] + "', '" + words[1] + "', '" + words[2] + "');"
      print (SQLString)

'''
      try:
        cur.execute(SQLString)
        print('Inserted')
      except:
        print('Insert failed')
'''

conn.commit()
cur.close()
conn.close()

    # EOF

=================输入文件==================

[pol@polhost dump]$ more dump_2018-05-18_A_Jade.1.ace 

 // Class Jade 

Jade "default"
Display  "Chromosome" "jade.maps.Vmap"
Display  "Map" "jade.maps.Vmap"
Display  "Table" "jade.maps.TableDisplay"
Display  "Cell" "lignage.Lignage"
Display  "Node" "lignage.Lignage"

 // End of this dump file 

[pol@polhost dump]$

===================================

1 个答案:

答案 0 :(得分:1)

我可以解释name conn not defined

try:
  conn = psycopg2.connect("dbname='test' user='postgres' host='localhost'")
  print ('Connected!')
except:
  print ('I am unable to connect to the database')

cur = conn.cursor()

try块中,您的代码尝试连接到数据库,但失败了。 在except块中,您编码打印I am unable to connect to the database ,然后继续。

在下一行中,您可以参考conn.cursor()。但conntry的原始分配失败,因此未进行分配,因此名称未定义。