如何根据项目位置运行不同的python版本?

时间:2019-05-13 10:44:19

标签: python cocos2d-x

我的系统是5.0.9-2-MANJARO

我已经设置了archwiki

中描述的python脚本解决方法
➜  ~ cat /home/nfl/bin/python 
#!/bin/bash
script=$(readlink -f -- "$1")
case "$script" in (/opt/cocos2d-x/*)
    exec python2 "$@"
    ;;
esac

exec python3 "$@"
➜  ~ which python
/home/nfl/bin/python
➜  ~ echo $PATH
/home/nfl/.nvm/versions/node/v10.15.3/bin:/home/nfl/bin:/usr/local/bin:/opt/cocos2d-x/tools/cocos2d-console/bin:/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin
➜  ~ 

当我运行输出当前python版本的python脚本时,它将在脚本(/ opt / cocos2d-x / *)中指定的目录中输出python2,而在未指定目录中输出python3。所以现在一切正常。

问题是当我尝试使用 PATH 运行sdkbox(cocos2d插件)时,似乎使用了python3而不是python2。

➜  ~ which sdkbox
/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package/sdkbox
➜  ~ sdkbox
RuntimeError: Bad magic number in .pyc file
➜  ~ 

但是当我直接从目录运行它时,它会起作用

➜  plugin_package pwd
/opt/cocos2d-x/tools/cocos2d-console/plugins/plugin_package
➜  plugin_package ./sdkbox 
  _______ ______  _     _ ______   _____  _     _
  |______ |     \ |____/  |_____] |     |  \___/ 
  ______| |_____/ |    \_ |_____] |_____| _/   \_
 Copyright (c) 2016-2018 SDKBOX Inc. v1.0.2.8
usage: sdkbox [-h] [-v] [-p [PROJECT]] [-b [PLUGIN]] [-D SYMBOL] [-i INPUT]
              [-o OUTPUT] [-q] [-d [DAYS]] [-l LEGACY] [--key KEY] [--dryrun]
              [--forcedownload] [--noupdate] [--alwaysupdate] [--patcherrors]
              [--nopatching] [--nopatchingcpp] [--jsonapi] [--forcecopy]
              [--mkey MKEY] [--mvalue MVALUE] [--local] [--remote]
              [--info INFO] [--runin RUNIN] [--apitoken APITOKEN]
              [--silenttime SILENTTIME] [--projectpath PROJECTPATH]
              [--platform PLATFORM]
              {import,info,update,forget,restore,list,clean,symbols,version,set,tracking,encrypt,decrypt}
sdkbox: error: too few arguments
➜  plugin_package 

究竟会发生什么以及如何解决?

2 个答案:

答案 0 :(得分:1)

Shebang可能是您的解决方案。它通过shebang处理python解释器。

Should I put #! (shebang) in Python scripts, and what form should it take?

答案 1 :(得分:0)

我建议为此使用虚拟环境。 virtualenv