过度使用 sitecustomize.py: .bash_profile 代替?

发布于 2024-12-15 10:24:17 字数 816 浏览 0 评论 0原文

也许是因为我更喜欢 python 而不是 shell,但我选择添加到 /usr/lib/python[xx]/sitecustomize.py 中的 sys.path 列表code> 以这种方式:

base = '/home/droogans/py/'

locs = ['foo','django']

for loc in locs:
    sys.path.insert(0, base + loc)

现在我在其下面添加了一个 try: except 块,以防我想在不使用 python manage.py shell 方法。

try:
    from django.core.management import setup_environ
    from website_foobar import settings
    setup_environ(settings)
except ImportError:
    pass

DjangoBook 有一个部分,“特殊的 python 提示”,建议您这样做,但建议使用 .bash_profile shell 脚本来执行该任务。使用它代替 sitecustomize.py 脚本是否有可衡量的好处?显然,我已经完成了,所以必须有某种令人信服的证据才能让我在谷歌上搜索一篇关于编写 shell 代码的文章。

Maybe it's because I am more comfortable with python over the shell, but I chose to add to my sys.path list in /usr/lib/python[x.x]/sitecustomize.py in this manner:

base = '/home/droogans/py/'

locs = ['foo','django']

for loc in locs:
    sys.path.insert(0, base + loc)

And now I've added a try:except block below it, in case I want to test out a template without using the python manage.py shell approach.

try:
    from django.core.management import setup_environ
    from website_foobar import settings
    setup_environ(settings)
except ImportError:
    pass

The DjangoBook has a section, "A special python prompt", that recommends you do this, but suggests using your .bash_profile shell script for the task. Is there a measurable benefit to utilizing that instead of a sitecustomize.py script? Obviously, I'm already done, so there'll have to be some kind of compelling evidence for me to google an article on writing shell code.

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

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

发布评论

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

评论(1

满地尘埃落定 2024-12-22 10:24:17

有时您在所使用的计算机上不会拥有超级用户状态。在这种情况下,您将无法修改 /usr,因此有必要知道如何在您的个人 ~/.bashrc 或 ~/.profile 或 ~/.bashrc 中设置环境变量。 bash_配置文件。 (要使用的特定文件取决于您的系统)。

这并不难做到。您需要添加的只是(我认为!)

PYTHONPATH=$HOME/py/foo:$HOME/py/django
DJANGO_SETTINGS_MODULE=website_foobar.settings

export PYTHONPATH DJANGO_SETTINGS_MODULE

Sometimes you are not going to have superuser status on the machine you are using. In such cases you won't be able to modify /usr, so it would be necessary then to know how to set environment variables in your personal ~/.bashrc or ~/.profile or ~/.bash_profile. (The particular file to use depends on your system).

It's not hard to do. All you'd need to add (I think!) is something like

PYTHONPATH=$HOME/py/foo:$HOME/py/django
DJANGO_SETTINGS_MODULE=website_foobar.settings

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