从shell运行Python脚本会调用错误的解释器

时间:2015-05-01 04:16:06

标签: python windows virtualenv

使用Windows 7,我安装了三个版本的Python。

  • C:\tools\python2\python.exe
  • C:\tools\python3\python.exe
  • C:\users\adsmith\.virtualenvs\flask_project\Scripts\python.exe

在我的virtualenv中运行where python确认了这一点。

C:\Users\adsmith\.virtualenvs\flask_project\Scripts\python.exe
C:\tools\python3\python.exe
C:\tools\python2\python.exe

在virtualenv中运行python会调用第一个解释器

(flask_project) C:\Users\adsmith\workspace\flask_project\flaskr>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Users\\adsmith\\.virtualenvs\\flask_project\\Scripts\\python.exe'

但是,如果我在virtualenv中创建一个.py文件并直接运行它,它将从系统Python 2解释器运行,我不知道为什么。正常运行python scriptname.py

(flask_project) C:\Users\adsmith>echo import sys; print(sys.executable) > tmp.py

(flask_project) C:\Users\adsmith>tmp.py
C:\tools\python2\Python.exe

(flask_project) C:\Users\adsmith>python tmp.py
C:\Users\adsmith\.virtualenvs\flask_project\Scripts\python.exe

供参考:

(flask_project) C:\Users\adsmith>assoc .py
.py=Python.File

(flask_project) C:\Users\adsmith>ftype Python.File
Python.File="python" "%1" %*

0 个答案:

没有答案