问题
I followed the following steps: [I am using Windows 8, Python 2.7.6]
- I created a virtual environment venv and then did pip install on requirements.txt to install all the libraries.
- 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.
- I have installed postgresql and added bin folder to environment path
- 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.
- 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