Django/Python初学者:执行Python管理时出错。py syncdb - psycopg2没有找到

时间:2022-03-30 18:22:14

I have Pythong2.6, psycopg2 and pgAdmin3 installed using Macports. My settings.py is:

我使用Macports安装了Pythong2.6、psycopg2和pgAdmin3。我的设置。py是:

DATABASE_ENGINE = 'postgresql_psycopg2'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mysite'             # Or path to database file if using sqlite3.
DATABASE_USER = 'postgres'             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

The error I get when I run python manage.py syncdb is:

我在运行python管理时遇到的错误。py syncdb是:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 221, in execute
    self.validate()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
    from django.db import models, connection
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 41, in <module>
    backend = load_backend(settings.DATABASE_ENGINE)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 17, in load_backend
    return import_module('.base', 'django.db.backends.%s' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 22, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2

Please note, I am a complete beginner in this stuff. I am originally a PHP-guy and trying out Python for a small personal project. Do I need to "turn on" Postgres?

请注意,我完全是这方面的初学者。我最初是一个PHP-guy,为一个小的个人项目尝试Python。我需要打开邮箱吗?

Also, when I sudo python manage.py runserver 8080 I get this error:

同样,当我支持python时。py runserver 8080我得到这个错误:

Validating models...
Unhandled exception in thread started by <function inner_run at 0x1242670>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
    from django.db import models, connection
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 41, in <module>
    backend = load_backend(settings.DATABASE_ENGINE)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 17, in load_backend
    return import_module('.base', 'django.db.backends.%s' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 22, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2

Please guide me. Any reply will be appreciated.

请指导我。如有任何答复,将不胜感激。

Thanks,

谢谢,

Wenbert!

Wenbert !

13 个解决方案

#1


32  

There seems to be a problem with your psycopg2 installation – Python does not find it. This is a Python installation problem, not a Django issue.

您的psycopg2安装似乎有一个问题——Python没有找到它。这是一个Python安装问题,而不是Django问题。

You can try to load it manually using the Python interpreter and see if it works:

您可以尝试使用Python解释器手动加载它,看看它是否有效:

$ python
>>> import psycopg2

If you get an ImportError exception, your installation is erroneous. To get a list of all directories Python looks for modules, use sys.path:

如果您获得了一个imporalterror exception异常,那么您的安装是错误的。要获取Python查找模块的所有目录列表,请使用sys.path:

$ python
>>> import sys
>>> print sys.path

You can also add custom directories to Python's module search path by modifying the sys.path variable. Do this somewhere before the respective import statement(s):

还可以通过修改sys向Python的模块搜索路径添加自定义目录。路径变量。请在有关的进口声明之前的某处这样做:

import sys
sys.path.append("my-path")

# ...
import psycopg2

#2


21  

If you have pip installed, simply install the missing extension by running:

如果您已经安装了pip,只需运行以下命令来安装丢失的扩展:

$ pip install psycopg2

#3


11  

For the record I got the same error for a different reason:

为了记录在案,我由于不同的原因得到了相同的错误:

I had put

我已经把

'ENGINE': 'django.db.backends.postgresql'

instead of

而不是

'ENGINE': 'django.db.backends.postgresql_psycopg2'

in settings.py

在settings.py

#4


6  

Follow the steps:

遵循的步骤:

  1. Install python libraries on your OS:
    • python-dev
    • python-dev
    • libpq-dev
    • libpq-dev
  2. 在OS上安装python库:python-dev libpq-dev
  3. Execute the command to install the psycopg2 library:
    • easy_install psycopg2
    • easy_install psycopg2
  4. 执行安装psycopg2库的命令:easy_install psycopg2

source: http://initd.org/psycopg/install/

来源:http://initd.org/psycopg/install/

#5


3  

Although you installed it, Python can apparently not find the module psycopg2. This is usually due to the module not being in Python's path. See if you can find a folder named psycopg2 in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages. If it's not there, did MacPorts tell you where it put psycopg2? If you can locate it, just move it to the site-packages directory and you should be fine.

虽然已经安装了它,但是Python显然无法找到psycopg2模块。这通常是因为模块不在Python的路径中。看看你是否能找到一个名为psycopg2的文件夹/库/框架/Python.framework/版本/2.6/lib/python2.6/site-package。如果它不在那里,MacPorts会告诉你它把psycopg2放在哪里了吗?如果您可以找到它,只需将它移动到站点包目录,您应该没问题。

#6


3  

I realized I didn't have psycopg2 installed

我意识到我没有安装psycopg2

aptitude install python-psycopg2

Worked like a charm

工作就像一个魅力

#7


1  

Yes Tim's answer works for me too.It works without prefix 'django.db.backends.' also. But remember to create database or schema you mentioned in settings.py:

是的,蒂姆的回答也适用于我。它没有前缀'django.db.backends。”也。但是记住要创建你在settings.y中提到的数据库或模式。

DATABASE_NAME = 'your_db_name' 

manually using your database client so when you run 'python manage.py syncdb' you don't get the same problem. I was stuck because i didn't create it manually. I got the same problem may be because I used buildout.

手动使用数据库客户端,因此当您运行“python管理”时。py syncdb,你不会遇到同样的问题。我被困住了,因为我没有手动创建它。我得到了同样的问题,可能是因为我使用了buildout。

#8


1  

I got the same error, but it was because I was using python26 ./manage.py runserver when my virtualenv only had python and python2.6 executables (thus the system python26 was being used, which didn't have psycopg2 installed

我犯了同样的错误,但这是因为我使用了python26。当我的virtualenv只有python和python2.6可执行文件时(因此使用了系统python26,它没有安装psycopg2

#9


1  

For me, psycopg2 was indeed installed, but not into the virtualenv in which Django was running. These two steps fixed it:

对我来说,psycopg2确实安装了,但是没有安装到Django正在运行的virtualenv中。这两个步骤修复了它:

sudo apt-get build-dep python-psycopg2
sudo /opt/myenv/bin/pip install psycopg2

#10


0  

Tim's answer worked for me too. By default, settings.py shows options like 'postgresql_psycopg2', 'mysql', etc, without a package name. Prefixing with 'django.db.backends.' worked for me (for postgresql_psycopg2, at least).

蒂姆的回答对我也有帮助。默认情况下,设置。py显示了“postgresql_psycopg2”、“mysql”等选项,没有包名。与“django.db.backends前缀。这对我很管用(至少对postgresql_psycopg2是这样)。

#11


0  

I had this issue recently after updating homebrew on OSX. psycopg2 was already listed in my virtualenv I just reinstalled psycopg2 and it worked again

最近我在OSX上更新了homebrew。psycopg2已经在我的virtualenv中被列出了,我只是重新安装了psycopg2,它又开始工作了。

pip install --forece-reinstall psycopg2

pip安装——forece-reinstall psycopg2

#12


0  

I'm on Windows and had installed psycopg2, but the 64 bit version. So my fix was to download the 32 bit one from here then in PowerShell with my virtual environment activated, my fix was:

我在Windows上安装了psycopg2,但是是64位版本。我的解决方案是从这里下载32位的然后在PowerShell中激活我的虚拟环境,我的解决方案是:

pip uninstall psycopg2
easy_install C:\WHEREVER_I_DOWNLOADED_IT\psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe

(The Windows Python 3.4 installer automatically installs easy_install and pip, and pip is the easiest way to remove the package, even if originally installed using easy_install.)

(Windows Python 3.4安装程序自动安装easy_install和pip, pip是删除包的最简单方法,即使最初是使用easy_install安装的。)

#13


0  

THIS HAS HELPED ME:

这帮助了我:

I just added PostgreSQL bin path to ENV and it was able to fine the needed dll: C:\Program Files (x86)\PostgreSQL\9.4\bin

我只是添加PostgreSQL本路径ENV,它能够好所需的dll:C:\Program Files (x86)\PostgreSQL\9.4\bin

https://groups.google.com/forum/#!topic/robotframework-users/qLq9KvHe1wE

https://groups.google.com/forum/ # ! / robotframework-users / qLq9KvHe1wE话题

#1


32  

There seems to be a problem with your psycopg2 installation – Python does not find it. This is a Python installation problem, not a Django issue.

您的psycopg2安装似乎有一个问题——Python没有找到它。这是一个Python安装问题,而不是Django问题。

You can try to load it manually using the Python interpreter and see if it works:

您可以尝试使用Python解释器手动加载它,看看它是否有效:

$ python
>>> import psycopg2

If you get an ImportError exception, your installation is erroneous. To get a list of all directories Python looks for modules, use sys.path:

如果您获得了一个imporalterror exception异常,那么您的安装是错误的。要获取Python查找模块的所有目录列表,请使用sys.path:

$ python
>>> import sys
>>> print sys.path

You can also add custom directories to Python's module search path by modifying the sys.path variable. Do this somewhere before the respective import statement(s):

还可以通过修改sys向Python的模块搜索路径添加自定义目录。路径变量。请在有关的进口声明之前的某处这样做:

import sys
sys.path.append("my-path")

# ...
import psycopg2

#2


21  

If you have pip installed, simply install the missing extension by running:

如果您已经安装了pip,只需运行以下命令来安装丢失的扩展:

$ pip install psycopg2

#3


11  

For the record I got the same error for a different reason:

为了记录在案,我由于不同的原因得到了相同的错误:

I had put

我已经把

'ENGINE': 'django.db.backends.postgresql'

instead of

而不是

'ENGINE': 'django.db.backends.postgresql_psycopg2'

in settings.py

在settings.py

#4


6  

Follow the steps:

遵循的步骤:

  1. Install python libraries on your OS:
    • python-dev
    • python-dev
    • libpq-dev
    • libpq-dev
  2. 在OS上安装python库:python-dev libpq-dev
  3. Execute the command to install the psycopg2 library:
    • easy_install psycopg2
    • easy_install psycopg2
  4. 执行安装psycopg2库的命令:easy_install psycopg2

source: http://initd.org/psycopg/install/

来源:http://initd.org/psycopg/install/

#5


3  

Although you installed it, Python can apparently not find the module psycopg2. This is usually due to the module not being in Python's path. See if you can find a folder named psycopg2 in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages. If it's not there, did MacPorts tell you where it put psycopg2? If you can locate it, just move it to the site-packages directory and you should be fine.

虽然已经安装了它,但是Python显然无法找到psycopg2模块。这通常是因为模块不在Python的路径中。看看你是否能找到一个名为psycopg2的文件夹/库/框架/Python.framework/版本/2.6/lib/python2.6/site-package。如果它不在那里,MacPorts会告诉你它把psycopg2放在哪里了吗?如果您可以找到它,只需将它移动到站点包目录,您应该没问题。

#6


3  

I realized I didn't have psycopg2 installed

我意识到我没有安装psycopg2

aptitude install python-psycopg2

Worked like a charm

工作就像一个魅力

#7


1  

Yes Tim's answer works for me too.It works without prefix 'django.db.backends.' also. But remember to create database or schema you mentioned in settings.py:

是的,蒂姆的回答也适用于我。它没有前缀'django.db.backends。”也。但是记住要创建你在settings.y中提到的数据库或模式。

DATABASE_NAME = 'your_db_name' 

manually using your database client so when you run 'python manage.py syncdb' you don't get the same problem. I was stuck because i didn't create it manually. I got the same problem may be because I used buildout.

手动使用数据库客户端,因此当您运行“python管理”时。py syncdb,你不会遇到同样的问题。我被困住了,因为我没有手动创建它。我得到了同样的问题,可能是因为我使用了buildout。

#8


1  

I got the same error, but it was because I was using python26 ./manage.py runserver when my virtualenv only had python and python2.6 executables (thus the system python26 was being used, which didn't have psycopg2 installed

我犯了同样的错误,但这是因为我使用了python26。当我的virtualenv只有python和python2.6可执行文件时(因此使用了系统python26,它没有安装psycopg2

#9


1  

For me, psycopg2 was indeed installed, but not into the virtualenv in which Django was running. These two steps fixed it:

对我来说,psycopg2确实安装了,但是没有安装到Django正在运行的virtualenv中。这两个步骤修复了它:

sudo apt-get build-dep python-psycopg2
sudo /opt/myenv/bin/pip install psycopg2

#10


0  

Tim's answer worked for me too. By default, settings.py shows options like 'postgresql_psycopg2', 'mysql', etc, without a package name. Prefixing with 'django.db.backends.' worked for me (for postgresql_psycopg2, at least).

蒂姆的回答对我也有帮助。默认情况下,设置。py显示了“postgresql_psycopg2”、“mysql”等选项,没有包名。与“django.db.backends前缀。这对我很管用(至少对postgresql_psycopg2是这样)。

#11


0  

I had this issue recently after updating homebrew on OSX. psycopg2 was already listed in my virtualenv I just reinstalled psycopg2 and it worked again

最近我在OSX上更新了homebrew。psycopg2已经在我的virtualenv中被列出了,我只是重新安装了psycopg2,它又开始工作了。

pip install --forece-reinstall psycopg2

pip安装——forece-reinstall psycopg2

#12


0  

I'm on Windows and had installed psycopg2, but the 64 bit version. So my fix was to download the 32 bit one from here then in PowerShell with my virtual environment activated, my fix was:

我在Windows上安装了psycopg2,但是是64位版本。我的解决方案是从这里下载32位的然后在PowerShell中激活我的虚拟环境,我的解决方案是:

pip uninstall psycopg2
easy_install C:\WHEREVER_I_DOWNLOADED_IT\psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe

(The Windows Python 3.4 installer automatically installs easy_install and pip, and pip is the easiest way to remove the package, even if originally installed using easy_install.)

(Windows Python 3.4安装程序自动安装easy_install和pip, pip是删除包的最简单方法,即使最初是使用easy_install安装的。)

#13


0  

THIS HAS HELPED ME:

这帮助了我:

I just added PostgreSQL bin path to ENV and it was able to fine the needed dll: C:\Program Files (x86)\PostgreSQL\9.4\bin

我只是添加PostgreSQL本路径ENV,它能够好所需的dll:C:\Program Files (x86)\PostgreSQL\9.4\bin

https://groups.google.com/forum/#!topic/robotframework-users/qLq9KvHe1wE

https://groups.google.com/forum/ # ! / robotframework-users / qLq9KvHe1wE话题