【问题标题】:Virtualenv exists but cannot access it from bash scriptVirtualenv 存在但无法从 bash 脚本访问它
【发布时间】:2013-10-28 23:54:22
【问题描述】:

我在 debian 服务器上设置了一个 django 项目,并安装了 virtualenvwrapper 等。它可以从命令行正常工作,但现在我想从脚本中激活它。脚本中的其他所有内容都可以正常工作,但 virtualenvwrapper 出现错误。

这是我的脚本:

#!/bin/bash

source /usr/local/bin/virtualenvwrapper.sh

NAME="mark"                                      # Name of the application
DJANGODIR=~/webapps                      # Django project directory
SOCKFILE=~/webapps/mark/run/gunicorn.sock        # we will communicte using this unix socket
USER=root                                            # the user to run as
GROUP=root                                                   # the group to run as
NUM_WORKERS=3                                                # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=mark.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=mark.wsgi                     # WSGI module name

echo "Starting $NAME"

# Activate the virtual environment
cd $DJANGODIR
workon mark
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ~/Envs/mark/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --log-level=debug \
  --bind=unix:$SOCKFILE

还有我的日志:

Starting mark
ERROR: Environment 'mark' does not exist. Create it with 'mkvirtualenv mark'.
2013-10-20 13:54:28 [31640] [INFO] Starting gunicorn 18.0
2013-10-20 13:54:28 [31640] [DEBUG] Arbiter booted
2013-10-20 13:54:28 [31640] [INFO] Listening at: unix:/root/webapps/mark/run/gunicorn.sock (31640)
2013-10-20 13:54:28 [31640] [INFO] Using worker: sync
2013-10-20 13:54:28 [31661] [INFO] Booting worker with pid: 31661
2013-10-20 13:54:28 [31662] [INFO] Booting worker with pid: 31662
2013-10-20 13:54:28 [31663] [INFO] Booting worker with pid: 31663

任何帮助将不胜感激!而且我认为使用我的 root 用户是不好的,但这是我第一次。

【问题讨论】:

  • 你以什么用户身份运行脚本
  • 感谢您的回复!好吧,在脚本中我说 USER=root 所以我希望是 root 用户,因为这也是我在终端中工作标记时登录的内容。
  • 好的,我尝试对此进行测试,我认为其中有提示。当我去的时候: echo "$USERNAME" 我什么也没得到,一个空行。当我把它放在我的脚本中时,我得到:root。我搞砸了我的用户不是吗...

标签: python django bash unix virtualenvwrapper


【解决方案1】:

试试这个来执行你的 bash 脚本:. ./script.sh

【讨论】:

    【解决方案2】:

    我在没有使用 workon 命令的情况下激活了它,并且成功了:

    source <path to your env>
    

    如果您需要环境路径,您可以这样做:

    workon <your env name>
    cdvirtualenv
    pwd
    

    还有你的路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 2011-10-19
      • 2015-06-15
      • 1970-01-01
      相关资源
      最近更新 更多