0

After trying to use pg library in Python as just as

import pg

I get this error message:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'pg'

how to import correctly the library?

Vzzarr
  • 4,600
  • 2
  • 43
  • 80
  • As @Vzzarr explained, you need to install PyGreSQL properly (this also includes installation of libpq) before you can use it. After proper installation you can import the "pg" (classic) and "pgdb" (DB-API 2) modules. – Cito Mar 09 '21 at 10:55

1 Answers1

3

The pg library has not the same name as the importing name so not very intuitive. According to the official documentation http://www.pygresql.org/contents/install.html in order to install it all you have to do is running

pip install PyGreSQL
Vzzarr
  • 4,600
  • 2
  • 43
  • 80
  • This is because PyGreSQL actually contains two top level modules, pg and pgdb. It is important to distinguish between the distribution name (PyGreSQL) and the top-level package name (pg and pgdb). Nowadays it is recommended to have only one top-level package with the same name as the distribution. But this is only a convention, and not all libraries follow it (PyGreSQL predates these conventions). – Cito Mar 09 '21 at 11:03