在shell脚本不可用virtualenvwrapper功能 [英] virtualenvwrapper functions unavailable in shell scripts

查看:1117
本文介绍了在shell脚本不可用virtualenvwrapper功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,再一次,我是一个不错的Python程序,这使得我的生活过的更轻松,节省了大量的时间。 Ofcourse,这涉及到一个virtualenv中,与virtualenvwrapper的 mkvirtualenv 函数的。该项目有几个必要的库一个requirements.txt文件(要求太:D),并计划将没有这些库运行

So, once again, I make a nice python program which makes my life ever the more easier and saves a lot of time. Ofcourse, this involves a virtualenv, made with the mkvirtualenv function of virtualenvwrapper. The project has a requirements.txt file with a few required libraries (requests too :D) and the program won't run without these libraries.

我想添加一个斌/运行应用程序可执行的shell脚本,这将是我的路径(符号链接实际上)。现在,这个剧本里面,我需要切换到的virtualenv之前,我可以运行这个程序。所以我把这个在

I am trying to add a bin/run-app executable shell script which would be in my path (symlink actually). Now, inside this script, I need to switch to the virtualenv before I can run this program. So I put this in

#!/bin/bash
# cd into the project directory
workon "$(cat .venv)"
python main.py

一个文件 .venv 包含的virtualenv名称。但是,当我运行此脚本,我得到 workon:找不到命令错误

A file .venv contains the virtualenv name. But when I run this script, I get workon: command not found error.

当然,我有virtualenvwrapper.sh源在我的.bashrc,但它似乎并没有在这个shell脚本是可用的。

Of course, I have the virtualenvwrapper.sh sourced in my bashrc but it doesn't seem to be available in this shell script.

所以,我怎样才能访问这些virtualenvwrapper功能吗?还是我这个做了错误的方式?你如何启动你的Python工具,每个都有自己的virtualenv!?

So, how can I access those virtualenvwrapper functions here? Or am I doing this the wrong way? How do you launch your python tools, each of which has its own virtualenv!?

推荐答案

单独的源代码脚本中的 virtualenvwrapper.sh 脚本导入virtualenvwrapper的功能。然后,您应该可以使用 workon 功能脚本。

Just source the virtualenvwrapper.sh script in your script to import the virtualenvwrapper's functions. You should then be able to use the workon function in your script.

也许更好,你可以创建一个shell脚本(你可以将其命名为 venv-run.sh 为例)运行任何Python脚本到给定的virtualenv和将其放置在的/ usr / bin中的/ usr / local / bin目录,或者是在你的<$任何目录C $ C> PATH 。

And maybe better, you could create a shell script (you could name it venv-run.sh for example) to run any Python script into a given virtualenv, and place it in /usr/bin, /usr/local/bin, or any directory which is in your PATH.

这样的脚本可能是这样的:

Such a script could look like this:

#!/bin/sh
# if virtualenvwrapper.sh is in your PATH (i.e. installed with pip)
source `which virtualenvwrapper.sh`
#source /path/to/virtualenvwrapper.sh # if it's not in your PATH
workon $1
python $2
deactivate

和可以简单地使用像 venv-run.sh my_virtualenv /path/to/script.py

这篇关于在shell脚本不可用virtualenvwrapper功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆