23

I installed psycopg2 on my Ubuntu Natty machine using apt-get. Now, I would like to know its version number. Can someone tell what the method to find version number for such python packages is.

Phani
  • 3,267
  • 4
  • 25
  • 50

2 Answers2

43

Since you installed it with the package manager, you can get the version from the command line with this command:

dpkg -s psycopg2

Alternatively you can get the version from using pip, if you have that installed

pip freeze | grep psycopg2

Or just run a python command to tell you:

python -c "import psycopg2; print(psycopg2.__version__)"

Output examples:

λ > pip freeze | grep psycopg2
psycopg2==2.4.4

λ > python -c "import psycopg2; print(psycopg2.__version__)"
2.4.4 (dt dec pq3 ext)
wkl
  • 77,184
  • 16
  • 165
  • 176
  • 2
    I like 1 line python commands. Thanks for that: $ python -c "import psycopg2; print(psycopg2.__version__)" – Love and peace - Joe Codeswell Sep 10 '15 at 15:49
  • getting this error : `AttributeError: module 'psycopg2' has no attribute '__version__'` when using `python -c "import psycopg2; print(psycopg2.__version__)"`. Why ? – kabrice Jan 06 '18 at 06:36
  • @kabrice That's strange - it's definitely still there in the latest version of it. You wouldn't happen to be using some kind of funky unicode `_` characters or not using double `_` are you? – wkl Jan 06 '18 at 12:52
  • for those who are using python 3, mac m1 pro. python3 -c "import psycopg2; print(psycopg2.__version__)" output: 2.8.6 (dt dec pq3 ext lo64) – that guy Feb 18 '22 at 16:06
3

You can check the __version__ attribute on the module, from within Python:

>>> psycopg2.__version__
'2.5 (dt dec pq3 ext)'
hbn
  • 1,846
  • 2
  • 12
  • 12