Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

what shebang to use for python scripts run under a pyenv virtualenv

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

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


So I made my example 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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...