如何在 shell 脚本中启动 Python 的虚拟环境 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lanqing
V2EX    Python

如何在 shell 脚本中启动 Python 的虚拟环境

  •  
  •   lanqing 2018-09-14 15:49:33 +08:00 5684 次点击
    这是一个创建于 2588 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的项目是用 python 的虚拟环境运行的,每次运行程序时运行
    1. cd 目录
    2.source .env/bin/activate
    3.celery worker ....
    就可以进入虚拟环境了.
    于是我写了个 shell 脚本,想自动启动这个程序

    #!/bin/bash
    PIDS=`ps -ef |grep celery worker -A crontabTask |grep -v grep | awk '{print $2}'`
    if [ "$PIDS" != "" ]; then
    echo "myprocess is runing!"
    else
    cd 目录
    source .env/bin/activate
    celery worker -A crontabTask -l INFO -B -f crontab.log
    #运行进程
    fi

    但是报错信息为,没有安装 python 的一个模块,这是因为虚拟环境没有进入的原因.
    请问这是为什么呀,该怎么解决呀
    10 条回复    2018-09-14 17:40:03 +08:00
    lanqing
        1
    lanqing  
    OP
       2018-09-14 15:59:04 +08:00
    刚没注意 ,第一个报错信息为 crontabShell.sh: source: not found
    第二个报错信息才是 python,模块没找到,
    这是因为 bash 中没有 source 命令呀!!??
    lanqing
        2
    lanqing  
    OP
       2018-09-14 16:15:09 +08:00
    问题已解决.
    ls -l /bin/sh 可以发现 ubuntu 的使用 dash 解析的.
    centos 使用 bash 解析的(bash 解析可以正常运行我的程序).
    原链接:
    https://blog.csdn.net/buynow123/article/details/51774018
    vissssa
        3
    vissssa  
       2018-09-14 16:16:56 +08:00
    .env/bin/celery 行么
    lanqing
        4
    lanqing  
    OP
       2018-09-14 16:22:03 +08:00
    @vissssa 这个问题我没有去试,但是我觉得对于我的程序来说是不可以的.
    因为这样虽然能够运行 celery ,但是程序中还是有其他的模块,这些模块都需要在虚拟环境中运行
    TimePPT
        5
    TimePPT  
    PRO
       2018-09-14 16:44:39 +08:00
    方便本地开发调试可以试试 direnv
    https://github.com/direnv/direnv
    ```
    direnv is an environment switcher for the shell. It knows how to hook into bash, zsh, tcsh, fish shell and elvish to load or unload environment variables depending on the current directory. This allows project-specific environment variables without cluttering the ~/.profile file.
    ```


    server 部署的话在我一般用 supervisor 惯了,在 conf 里可以设 env path
    fuxiuyin
        6
    fuxiuyin  
       2018-09-14 17:07:30 +08:00   1
    脚本里面直接用
    {path_to_dir}/env/bin/[python|celery]这种执行别
    fuxiuyin
        7
    fuxiuyin  
       2018-09-14 17:09:29 +08:00   1
    不知道 celery 能不能用 python -m celery 这种。
    我用 virtualenv 的时候,就是直接 xxx/env/bin/python xxxx 这样跑,这样 python 能自己找自己的环境,指定那个虚拟环境的 python 就能加载哪个环境的包依赖。
    lanqing
        8
    lanqing  
    OP
       2018-09-14 17:35:18 +08:00
    @TimePPT 好的!还没有学习 supervisor 和 direnv,以后会看看的
    lanqing
        9
    lanqing  
    OP
       2018-09-14 17:39:44 +08:00
    @vissssa 试了一下,可以= =
    lanqing
        10
    lanqing  
    OP
       2018-09-14 17:40:03 +08:00
    @fuxiuyin 试了一下 确实可以!感谢指教
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1124 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 17:46 PVG 01:46 LAX 10:46 JFK 13:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86