在 pyenv virtualenv 下运行的 python 脚本使用什么shebang [英] what shebang to use for python scripts run under a pyenv virtualenv

查看:32
本文介绍了在 pyenv virtualenv 下运行的 python 脚本使用什么shebang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 Python 脚本应该从 pyenv virtualenv 运行时,文件的正确shebang 是什么?

When a python script is supposed to be run from a pyenv virtualenv what is the correct shebang for the file?

作为示例测试用例,我的系统 (OSX) 上的默认 python 没有安装 pandas.pyenv virtualenv venv_name 可以.我尝试从 virtualenv 中获取 python 可执行文件的路径.

As an example test case, the default python on my system (OSX) does not have pandas installed. The pyenv virtualenv venv_name does. I tried getting the path of the python executable from the virtualenv.

$ pyenv activate venv_name
(venv_name)$ which python
/Users/username/.pyenv/shims/python


所以我做了我的例子 script.py:

#!/Users/username/.pyenv/shims/python
import pandas as pd
print 'success'


但是当我尝试运行脚本时,出现错误:


But when I tried running the script, I got an error:

(venv_name) $ ./script.py
./script.py: line 2: import: command not found
./script.py: line 3: print: command not found


尽管在命令行上运行该路径可以正常工作:


Although running that path on the command line works fine:

(venv_name) $ /Users/username/.pyenv/shims/python script.py
success

(venv_name) $ python script.py # also works
success

什么是合适的shebang?理想情况下,我想要一些通用的东西,以便它指向我当前的 venv 的 python.

What is the proper shebang for this? Ideally, I want something generic so that it will point at the python of whatever my current venv is.

推荐答案

我真的不知道为什么用完整路径调用解释器对你不起作用,我一直使用它,但如果你想使用您环境中的python解释器,您应该这样做:

I don't really know why calling the interpreter with the full path wouldn't work for you, I use it all the time, but if you want to use the python interpreter that is in your environment you should do:

#!/usr/bin/env python

这样你就可以在你的环境中搜索要使用的 python 解释器.

That way you search your environment for the python interpreter to use.

这篇关于在 pyenv virtualenv 下运行的 python 脚本使用什么shebang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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