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 · 5160 次点击
    这是一个创建于 2023 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的项目是用 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  
       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   ·   我们的愿景   ·   实用小工具   ·   2928 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:04 · PVG 22:04 · LAX 07:04 · JFK 10:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.