0

I'm trying to connect to a postgres database with psycopg2:

def get_posts():
 db = psycopg2.connect(database=DBNAME)
 c = db.cursor()

Which returns:

  File "/vagrant/forum/solution/forumdb_solved.py", line 9, in get_posts
  db = psycopg2.connect(database=DBNAME)
  AttributeError: 'module' object has no attribute 'connect'

I have researched other solutions to this problem (Psycopg2: 'module' object has no attribute 'connect'). None of my folder/file structure contains psycopg2 though. The other issue which seems to come up is read/write privileges of the folder in which psycopg2 is installed. I have psycopg2 installed in multiple locations (don't ask!) and not sure how to check this. Any help would be appreciated by a total newbie.

  • What version of Python are you using? – Ziyad Edher May 17 '18 at 18:01
  • Hi @ZiyadEdher I'm using Python 2.7. I'm running it on a virtual machine on my laptop(windows). – Niall O Hara May 17 '18 at 18:04
  • 1
    Did you _ever_ have anything called `psycopg2` in any of your files? – Ziyad Edher May 17 '18 at 18:04
  • Whether it is a Python file `psycopg2.py` or otherwise? (Obviously other than `import psycopg2`). – Ziyad Edher May 17 '18 at 18:05
  • 1
    Please edit to your question the output from `print psycopg2`. – Ilja Everilä May 17 '18 at 18:05
  • Not that I know about. I have checked all my immediate files and folders. – Niall O Hara May 17 '18 at 18:08
  • @IljaEverilä How do I go about doing this? Can I do it in the command line print(psycpog2)? What information are you looking for? Apologies for the stupid questions. – Niall O Hara May 17 '18 at 18:19
  • Just `print psycopg2` directly after importing it. – Ziyad Edher May 17 '18 at 18:20
  • Given that the program runs in the same environment, you could do it in a shell as well. But if you're unsure, add a print somewhere before the line where it crashes (or as @ZiyadEdher pointed out, right after the import). I'm looking for what your `psycopg2` object is, actually. It should print out something along the lines of ``, if all's well. – Ilja Everilä May 17 '18 at 18:21
  • @IljaEverilä this is the output from print psycopg2. – Niall O Hara May 17 '18 at 18:29
  • 1
    Is the code you posted the entire code? Try to import the module in the interactive interpreter and trying calling `psycopg2.connect`. – Ziyad Edher May 17 '18 at 18:30
  • @ZiyadEdher I tried to import the module in the interactive interpreter and calling psycopg2.connect but it is giving me the same error. – Niall O Hara May 17 '18 at 18:43
  • I'd almost go as far as to say that your installation is somehow corrupted. Please open up `/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py` in your favourite editor and inspect if it looks ok (has the function `connect()` to begin with). – Ilja Everilä May 17 '18 at 18:45
  • 1
    @IljaEverilä I have found a solution to my problem. I believe it was due to restricted user permissions on the /usr/local/lib folder. I ended up re-installing psycopg2 using a sudo install and and everything seems to be working fine now. This is my first StackOverflow question...how do I thank the contributors and close the question properly? Thanks a million for your patience :) – Niall O Hara May 17 '18 at 18:58
  • You could write up a self answer; it's perfectly ok to do so. – Ilja Everilä May 17 '18 at 19:10

0 Answers0