I have psycopg2 installed in virtual env, but still getting importerror psycopg2

点点圈 提交于 2019-12-25 06:47:32

问题


I followed the following steps: [I am using Windows 8, Python 2.7.6]

  1. I created a virtual environment venv and then did pip install on requirements.txt to install all the libraries.
  2. I had to explicitly create a file called config.py to set the os.environ variables. Then, I am doing import config in other files.
  3. I have installed postgresql and added bin folder to environment path
  4. psycopg2 was failing to install from running requirements.txt. So, I downloaded the zip with setup.py from online, and ran the setup.py in the virtial environment. Later when I did "pip install psycopg2" in the virtual env, it said requirement already satisfied.
  5. Now I ran venv/Scripts/.py and got the following error:
Traceback (most recent call last):
  File "remote-alert.py", line 5, in <module>
    from database import db_session
  File "d:\Documents\Tutorials\Technologies\inMobi\soynerdito\HobbesAlert\databa
se.py", line 12, in <module>
    isolation_level="READ UNCOMMITTED" )
  File "d:\Documents\Tutorials\Technologies\inMobi\soynerdito\HobbesAlert\venv\l
ib\site-packages\sqlalchemy\engine\__init__.py", line 344, in create_engine
    return strategy.create(*args, **kwargs)
  File "d:\Documents\Tutorials\Technologies\inMobi\soynerdito\HobbesAlert\venv\l
ib\site-packages\sqlalchemy\engine\strategies.py", line 73, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "d:\Documents\Tutorials\Technologies\inMobi\soynerdito\HobbesAlert\venv\l
ib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 401, in dbapi

import psycopg2
ImportError: No module named psycopg2

In the error output, the file psycopg2.py, which is being accessed exists in the given path. Then why I am getting the error?

I checked many solutions given online, like postgresql must be installed with bin folder in PATH. I should use only virtual env python.exe to run my program, I should not run it as a superuser, etc. Nothing worked for me till now.

Please help!


回答1:


Unfortunately, psycopg2 needs to compile on each architecture; and I am sure if you checked the output of pip install psycopg2 you would have noticed some errors (even though, in the end, it will end up installing it).

It is best to install the pre-compiled windows binary, which you can find at this website.




回答2:


I had the same problem in windows and since i had installed psycopg2 both on the system python and in virtual environment, I found that with LIB/site-packages both had a folder named psycopg2 though within the virtual environment the contents were fewer.

It worked well when I copied the contents of the system Lib\site-packages\psycopg2 to those of the corresponding file in virtual environment.



来源:https://stackoverflow.com/questions/26314044/i-have-psycopg2-installed-in-virtual-env-but-still-getting-importerror-psycopg2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!