Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

python - ModuleNotFoundError: No module named 'psycopg2._psycopg' (Ipython)

This question has been asked before but none of the answers are satisfactory.

Using Command Prompt on Windows 10, there is no issue importing Psycopg2, which is located in

C:UsersmyusernameAppDataRoamingPythonPython39site-packages

For example:

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>>

In the IPython console of Spyder, I get an error:

Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.19.0 -- An enhanced Interactive Python.

In [1]: import psycopg2
Traceback (most recent call last):

  File "<ipython-input-1-7d2da0a5d979>", line 1, in <module>
    import psycopg2

  File "C:UsersmyusernameAppDataRoamingPythonPython39site-packagespsycopg2\__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa

ModuleNotFoundError: No module named 'psycopg2._psycopg'

Running sys.path indicates that the location of the package installation is there.

In [4]: sys.path
Out[4]: 
['C:\Program Files\Spyder\Python\python37.zip',
 'C:\Program Files\Spyder\Python',
 'C:\Program Files\Spyder\pkgs',
 'C:\Program Files\Spyder\pkgs\IPython\extensions',
 'C:\Users\myusername\AppData\Roaming\Python\Python39\site-packages\psycopg2',
 '',
 'C:\Users\myusername\AppData\Roaming\Python\Python39\site-packages',
 'C:\Users\myusername\.ipython']

What do I need to do to make this work? It only recently started happening when I reset Windows 10 (uninstalls the operating system and reinstalls it from the cloud, while keeping your files).

This is a standalone installation of Spyder without Anaconda.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As we can see in your path, you have two versions of python installed in your machine.

One points to:

C:UsersmyusernameAppDataRoamingPythonPython39site-packages

which is python 3.9.

Another contains the spider code, which is python 3.7.

You just have to install your Psycopg2 in python 3.7 instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...