powershell和python哪个好用_Powershell中的Python 2和3

I've installed Python 3.5 using Anaconda and 2.7 using the following command:

conda create -n py27 python=2.7 anaconda

This successfully installed 2.7 as a separate environment with the Anaconda3 folder:

# original installation path for 3.5

C:\Users\username\AppData\Local\Continuum\Anaconda3

# path for 2.7 after conda create -n is run

C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\py27

This works great as long as I'm using an IDE like Spyder or IPython Notebook, where I can choose which Python version. However, I have not been able to use py27 (Python 2.7) in the PowerShell. I've tried using:

activate py27

yet python command still fires up 3.5 and py27 causes this error:

py27 : The term 'py27' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:1 + py27

+ ~~~~

+ CategoryInfo : ObjectNotFound: (py27:String) [], CommandNotFoundException

+ FullyQualifiedErrorId : CommandNotFoundException

I've tried everything I know or have read here on SO, but nothing I've tried has been able to resolve the issue. I'm not sure if this helps, but here is my PATH variable for reference:

C:\Users\username\AppData\Local\Continuum\Anaconda3;C:\Users\username\AppData\Local\Continuum\Anaconda3\Scripts;C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;[stuff in between];C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27;C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27\Scripts\;C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27\Library\bin

解决方案

I've decided to close this question with the following solution: use Cmd.exe or use the PowerShell with Bill Stewart's Invoke-CmdScript (see comments). First, the simpler solution using the Command Prompt (Cmd.exe):

# to activate

C:\Users\me\> activate py2

# to deactivate and return to Python 3.5

C:\Users\me\> deactivate

Which activates the Python 2.7 environment I created using conda. Now, for the Powershell version. First, go this site and download the Environment.ps1 script. For me, it downloaded as text file, so I opened the file, removed the .txt extension and replaced it with .ps1. So, let's say I saved Environment.ps1 in the following directory C:\Users\user_name\AppData\Local\invoke_cmdscript. Take this path and add it to the PATH user variable within the Environment variables form. Now, the commands to type in your PowerShell window:

# dot-source my file

. Environment.ps1

# activate 2.7 environment (if not in the directory of activate.bat, specify full path)

Invoke-CmdScript .\activate.bat py2

# deactivate 2.7 environment (same note about current directory and full paths)

Invoke-CmdScript .\deactivate.bat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值