简体   繁体   English

没有名为“psycopg2”的模块

[英]No module named 'psycopg2'

I Installed psycopg2 using pip我使用 pip 安装了 psycopg2

C:\Users\username>python -m pip install psycopg2

Adjusted my settings to:将我的设置调整为:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # also tried: django.db.backends.postgresql
    'NAME': 'name',
    'USER': 'user',
    'PASSWORD': 'my_password',
    'HOST': 'localhost',
    'PORT': '5432',
   }
}

When I attempt to run the server:当我尝试运行服务器时:

import psycopg2 as Database ImportError:导入 psycopg2 作为数据库导入错误:

No module named 'psycopg2'没有名为“psycopg2”的模块

So I ran this again and got:所以我再次运行并得到:

python -m pip install psycopg2 Requirement already satisfied (use --upgrade to upgrade): psycopg2 in ... python -m pip install psycopg2 要求已经满足(使用--upgrade升级):psycopg2 in ...

So what have I done wrong?那么我做错了什么? I am new to Django我是 Django 的新手

There is a possibility that you have two versions of python installed and python2-pip is aliased as pip while python3-pip is aliased as pip3 .有可能您安装了两个版本的 python 并且python2-pip别名为pippython3-pip别名为pip3

Make sure you are using right version with python and pip .确保您使用正确的版本与pythonpip

So the command would be:所以命令是:

pip3 install psycopg2

你可以试试

pip install psycopg2-binary

You may need to install the package from whl file, try the below command:您可能需要从whl文件安装软件包,请尝试以下命令:

pip install psycopg2-2.6.2-cp27-cp27m-win_amd64.whl

You can download this package from http://www.lfd.uci.edu/~gohlke/pythonlibs/你可以从http://www.lfd.uci.edu/~gohlke/pythonlibs/下载这个包

在 mac-os 上 pip install psycopg2-binary 将解决这个问题

It is possible that while following the installation tutorial you installed virtualenv , created some environment (like env1 ) and followed on to install django on that environment.有可能在按照安装教程安装virtualenv ,创建了一些环境(如env1 ),然后在该环境中安装 django。

If so, than you are running django on that environment as well (otherwise it would not be installed).如果是这样,那么您也在该环境中运行 django(否则将不会安装)。 In order to install modules you need to first activate the environment and then use pip .为了安装模块,您需要先激活环境,然后使用pip

Install below dependency before installing psycopg2在安装 psycopg2 之前安装以下依赖项

apt-get install libpq-dev

Now install:现在安装:

pip install psycopg2

Give the below command to generate DB for your Django config给出以下命令为您的 Django 配置生成数据库

python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM