简体   繁体   English

如何从子目录导入 Django 项目 settings.py Python 文件?

[英]How to import a Django project settings.py Python file from a sub-directory?

I created a sub-directory of my Django project called bin where I want to put all command-line run Python scripts.我创建了一个名为bin的 Django 项目的子目录,我想在其中放置所有命令行运行 Python 脚本。 Some of these scripts need to import my Django project settings.py file that is in a parent directory of bin .其中一些脚本需要导入我的 Django 项目settings.py文件,该文件位于bin的父目录中。

How can I import the settings.py file from a sub-directory of the project?如何从项目的子目录中导入settings.py文件?

The code that I use in my command-line script to set into the "Django context" of the project is:我在命令行脚本中用于设置项目的“Django 上下文”的代码是:

from django.core.management import setup_environ
import settings
setup_environ(settings)

This works fine if the script is in the root directory of my project.如果脚本在我的项目的根目录中,这很好用。

I tried the following two hacks to import the settings.py file and then setup the project:我尝试了以下两个技巧来导入settings.py文件,然后设置项目:

import os
os.chdir("..")

import sys
sys.path = [str(sys.path[0]) + "/../"] + sys.path

The cruel hack can import settings.py , but then I get the error:残酷的黑客可以导入settings.py ,但后来我得到了错误:

project_module = __import__(project_name, {}, {}, [''])
ValueError: Empty module name

I think your approach may be over-complicating something that Django 1.x provides for you.我认为您的方法可能会使 Django 1.x 为您提供的东西过于复杂。 As long as your project is in your python path, you can set the environment variable DJANGO_SETTINGS_MODULE at the top of your script like so:只要您的项目位于 python 路径中,您就可以在脚本顶部设置环境变量 DJANGO_SETTINGS_MODULE,如下所示:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

In your command line script where you need to read your settings, simply import the settings module from 'django.conf' as you would do in your application code:在需要读取设置的命令行脚本中,只需从“django.conf”导入设置模块,就像在应用程序代码中所做的那样:

from django.conf import settings

And presto, you have your settings and a Django-enabled environment for your script.并且很快,您就有了脚本的设置和支持 Django 的环境。

I personally prefer to set my DJANGO_SETTINGS_MODULE using '/usr/bin/env' in a bash script called 'proj_env' so I don't have to repeat it我个人更喜欢在名为“proj_env”的 bash 脚本中使用“/usr/bin/env”设置我的 DJANGO_SETTINGS_MODULE,所以我不必重复它

#!/bin/bash

proj_env="DJANGO_SETTINGS_MODULE=myproject.settings"

/usr/bin/env $proj_env ${*}

With this, now I can run any python script with my Django application in context:有了这个,现在我可以在上下文中使用我的 Django 应用程序运行任何 python 脚本:

proj_env python -m 'myproject.bin.myscript'

If you use virtualenv, this also gives you a good place to source the activate script.如果您使用 virtualenv,这也为您提供了一个获取激活脚本的好地方。

etc. etc.等等等等

This is going one level up from your question, but probably the best solution here is to implement your scripts as custom manage.py (django-admin.py) commands .这比您的问题更上一层楼,但这里最好的解决方案可能是将您的脚本实现为自定义 manage.py (django-admin.py) commands This gives you all of Django's functionality (including settings) for free with no ugly path-hacking, as well as command-line niceties like options parsing.这为您免费提供了 Django 的所有功能(包括设置),没有丑陋的路径黑客,以及像选项解析这样的命令行细节。 I've never seen a good reason to write Django-related command-line scripts any other way.我从未见过以其他方式编写与 Django 相关的命令行脚本的充分理由。

Add the parent directory to your path:将父目录添加到您的路径:

import sys
sys.path.append('../')
import settings

Update from comments:评论更新:

Don't forget the __init__.py file in the directory that has your settings.py – S.Lott不要忘记包含您的 settings.py 的目录中的__init__.py文件 – S.Lott

Let's say your project directory is /opt/someProject/`假设您的项目目录是 /opt/someProject/`

This has files like:这有如下文件:

manage.py管理.py

Now you have you may have your sub directory anywhere, does not matter.现在你有了你可能在任何地方都有你的子目录,没关系。 Eg.例如。 subdirectory could be like:子目录可能像:

/opt/someproject/dir1/dir2

Now for you to import your project settings inside /opt/someProject/dir1/dir2现在让您在 /opt/someProject/dir1/dir2 中导入您的项目设置

You need to set your PYTHONPATH variable您需要设置 PYTHONPATH 变量

export PYTHONPATH=/opt/someproject/导出 PYTHONPATH=/opt/someproject/

Now to import modules from bin现在从 bin 导入模块

from someproject import bin从某个项目导入箱

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

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