使用manage.pysyncdb时,Django在virtualenv中找不到应用程序

发布于 2024-09-16 00:21:08 字数 306 浏览 4 评论 0原文

我的问题是让manage.pysyncdb在virtualenv中运行。

它曾经工作得很好,但当我安装 South 并更新 pip 和分发时,它似乎在某个时候出现了故障。

无论如何,当激活 virtualenv 时,我可以在交互式解释器中很好地导入应用程序。通过 mod_wsgi 运行,应用程序也会导入,并且站点可以运行。

当我运行manage.pysyncdb时,它无法在我的virtualenv中的INSTALLED_APPS中找到任何应​​用程序。它可以很好地拾取系统安装的应用程序,但当它尝试仅导入 virtualenv 应用程序时会失败。

My problem is in getting manage.py syncdb to run within a virtualenv.

It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.

Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.

When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-09-23 00:21:08

嗨,这是一个老问题,但看到它没有得到解答。不确定您要做什么,但基本上有两种模式可以使用 virtualenv,

  1. 对于开发,创建独立的环境
  2. 对于部署,创建独立的环境

在第一种情况下,您需要首先激活 virtualenv使用源 venv/bin/activate,在部署时,您需要确保为您的网站代码激活 virtualenv。就我个人而言,我更喜欢以下方法来确保您的路径设置正确。 (我在开发时也将其添加到我的manage.py中,所以我不必担心首先激活环境。

修改后的manage.py

#!/usr/bin/env python
import os.path

# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from django.core.management import execute_manager
try:
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings)

这有效,由于我构建项目的方式,你必须将其更改为你的目录我的项目的结构如下:

TopLevelDir
|
|- Project DIR
|- venv
|- requirements 
|- deployment configs

Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,

  1. For development, to create self-contained environments
  2. For deployment, to create self-contained environments

In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.

Modified manage.py

#!/usr/bin/env python
import os.path

# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from django.core.management import execute_manager
try:
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings)

This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:

TopLevelDir
|
|- Project DIR
|- venv
|- requirements 
|- deployment configs
秉烛思 2024-09-23 00:21:08

我有一个简单的解决方案,

只需从虚拟环境的 bin 中的 python 启动 manage.py 即可。

所以说你的Python在这里 /home/tom/environments/my_env/bin/python 你可以像这样启动manage.py:

/home/tom/environments/my_env/bin/python manage.pysyncdb

然后创建一个符号链接到在你的 django 项目中创建虚拟环境的 python 并将其命名为 env_python 然后你可以这样做:

./env_python manage.pysyncdb

I have a simple solution to this

Just launch manage.py from the python in the bin of your virtual environment.

So say your python is here /home/tom/environments/my_env/bin/python you could launch manage.py like so:

/home/tom/environments/my_env/bin/python manage.py syncdb

then just create a symlink to the virtual environment's python inside your django project and call it env_python then you can do this:

./env_python manage.py syncdb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文