使用manage.py syncdb时,Django没有在virtualenv中找到应用程序

时间:2022-12-12 18:04:38

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

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

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

它在某一点上工作得很好,但是当我安装South并更新pip并分发时,似乎已经破坏了。

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.

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

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.

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

2 个解决方案

#1


5  

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,

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

  1. For development, to create self-contained environments
  2. 为了开发,创建自包含的环境

  3. For deployment, to create self-contained environments
  4. 用于部署,以创建自包含环境

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.

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

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

#2


3  

I have a simple solution to this

我有一个简单的解决方案

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

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

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

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

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

/ 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:

然后在django项目中创建一个符号链接到虚拟环境的python,并将其命名为env_python,然后就可以执行以下操作:

./env_python manage.py syncdb

./env_python manage.py syncdb

#1


5  

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,

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

  1. For development, to create self-contained environments
  2. 为了开发,创建自包含的环境

  3. For deployment, to create self-contained environments
  4. 用于部署,以创建自包含环境

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.

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

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

#2


3  

I have a simple solution to this

我有一个简单的解决方案

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

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

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

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

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

/ 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:

然后在django项目中创建一个符号链接到虚拟环境的python,并将其命名为env_python,然后就可以执行以下操作:

./env_python manage.py syncdb

./env_python manage.py syncdb