npm使用2个版本的Python

每当我安装某些node包时,就会出现涉及Python版本的错误。 这是因为我有Python 2.7Python 3.3 (这些是我上次更新的最新版本,但现在有3.4 ),但显然我只能在我的PATH (任何后来的文件夹被重写)源只有一个版本。 我的PATHPython 3.3 ,因为它比较新,但是在5年之后,仍然有很多程序,包括使用Python 2 npm模块。

有什么办法可以包含一个“fallback” Python版本,对于那些不能使用版本3的模块呢? 一般的解决scheme将是惊人的,但我至less会喜欢安装npm模块的解决scheme。 请记住,有些模块对于Python 3可能是完美的,但我不确定是否有任何不能使用Python 2 。 有了这个说法,一个解决scheme,允许使用最新的版本,当兼容和一些旧版本时,不是最好的。

作为参考,我的Python 2.7安装在C:\Python27\python文件夹中, Python 3.3安装在C:\Python27\python文件夹中。 我得到的npm错误是:

contextify@0.1.8安装c:\ repos \ konneka \ node_modules \ buster \ node_modules \ bus ter-syntax \ node_modules \ jsdom \ node_modules \ contextify node-gyp rebuild

 | c:\repos\konneka\node_modules\buster\node_modules\buster-syntax\node_modules\jsd om\node_modules\contextify>node "c:\Program Files\nodejs\node_modules\npm\bin\no de-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild when@1.3.0 node_modules\buster\node_modules\buster-server-cli\node_modules\buste r-cli\node_modules\buster-configuration\node_modules\when lodash@0.5.2 node_modules\buster\node_modules\buster-test-cli\node_modules\ramp\ node_modules\ramp-resources\node_modules\lodash gyp ERR! configure error gyp ERR! stack Error: Python executable "python" is v3.3.2, which is not support ed by gyp. gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0. gyp ERR! stack at failPythonVersion (c:\Program Files\nodejs\node_modules\np m\node_modules\node-gyp\lib\configure.js:108:14) gyp ERR! stack at c:\Program Files\nodejs\node_modules\npm\node_modules\node -gyp\lib\configure.js:97:9 gyp ERR! stack at ChildProcess.exithandler (child_process.js:645:7) gyp ERR! stack at ChildProcess.emit (events.js:98:17) gyp ERR! stack at maybeClose (child_process.js:755:16) gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:822: 5) ERR! System Windows_NT 6.2.9200 command "node" "c:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\nod e-gyp\\bin\\node-gyp.js" "rebuild" esprima-fb@3001.1.0-dev-harmony-fb node_modules\browserify\node_modules\syntax-e rror\node_modules\esprima-fb http-proxy@0.10.4 node_modules\karma\node_modules\http-proxy ├── pkginfo@0.3.0 └── utile@0.2.1 (deep-equal@0.2.1, async@0.2.10, ncp@0.4.2, i@0.3.2, mkdirp@0.5. 0) cwd c:\repos\konneka\node_modules\buster\node_modules\buster-syntax\node_module s\jsdom\node_modules\contextify gyp ERR! node -v v0.10.29 gyp ERR! node-gyp -v v0.13.1 gyp ERR! not ok 

使用virtualenv和nodeenv,并有你想要的任何版本的组合。

我在他们之间翻转的方式是在python文件的开头使用shebang

通过包括:

 #!python2 

在文件的开头,当你双击它时,py.exe(或pyw.exe)将它传递给那个版本的python(假设它可以find它),你甚至可以传递它:

 #!python2.7 

要明确使用2.7版,假设你安装了多个版本的python2。

并在安装模块时使用它,只需编辑安装文件以将该行作为第一行,然后避免将python放入命令行

假设您使用batch file来安装像我这样的模块,大多数人都会这样做:

 python setup.py install 

然后我使用:

 setup.py install 

这可以确保安装脚本指定安装到哪个版本的python

我也发现了一段时间以来,杂耍python2 + 3,它使我更容易编辑registry,当我右键单击一个Python文件,我可以打开它闲置2或3。

编辑:我应该提到,这只有当python3是最后一次安装正常工作,因为python 2在registry中设置不友好的值,并直接传递到python2版本,而不是先检查文件