0

I am trying to use some code that requires Python3

I already use Python (version 2) for something else so I got things working by using python3 instead of python when installing various modules

sudo apt install python3-docopt
sudo apt install python3-requests
sudo apt install python3-lxml
sudo apt install python3-psycopg2
sudo apt install python3-pip
sudo pip3 install tqdm

and that worked for the first program I run. But now another program (from the same repository) complains about sql module within psycopg2, although psycopg2 and sql modules are imported.

ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ sudo pip3 install sql
Requirement already satisfied (use --upgrade to upgrade): sql in /usr/local/lib/python3.4/dist-packages
Cleaning up...
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ sudo apt install python3-sql
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-sql is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 359 not upgraded.
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ python3 discogs-import.py ../csv/*
    Traceback (most recent call last):
      File "discogs-import.py", line 6, in <module>
        from psycopg2 import sql
    ImportError: cannot import name 'sql'

I dont understand what I have to do to resolve this ?

Trying to do form console also failed

ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ python3
Python 3.4.3 (default, Nov 12 2018, 22:25:49)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from psycopg2 import sql
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'sql'
>>> from python3-psycopg2 import sql
  File "<stdin>", line 1
    from python3-psycopg2 import sql
                ^
SyntaxError: invalid syntax


ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ sudo apt install python3-psycopg2
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-psycopg2 is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 359 not upgraded.
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ pip3 show python3-psycopg2
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ pip3 show psycopg2
---
Name: psycopg2
Version: 2.4.5
Location: /usr/lib/python3/dist-packages
Requires:
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$ pip show psycopg2
---
Name: psycopg2
Version: 2.4.5
Location: /usr/lib/python2.7/dist-packages
Requires:
ubuntu@ip-172-31-39-147:~/code/discogs-xml2db/speedup$
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

1 Answers1

-1

You need to import it in a different way:

from psycopg2 import sql

The result is:

Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from psycopg2 import sql
>>> 

Furthermore take care of selecting the right python environment.
It might result in complications installing packages through pip with sudo.

Using sudo pip installs into the root user environment while using just python script.py to call your script uses the user environment.


I installed it into a virtual environment using:

pip install psycopg2-binary
Nils
  • 2,665
  • 15
  • 30