Powershell中的Python 2和3 [英] Python 2 and 3 from Powershell

查看:140
本文介绍了Powershell中的Python 2和3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Anaconda安装了Python 3.5,并使用以下命令安装了2.7:

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

conda create -n py27 python=2.7 anaconda

这已成功将2.7安装为带有Anaconda3文件夹的单独环境:

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

只要我使用的是像Spyder或IPython Notebook这样的IDE,就可以很好地工作,在这里我可以选择哪个Python版本.但是,我无法在PowerShell中使用py27(Python 2.7).我尝试使用:

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

但是python命令仍然会触发3.5,而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

我已经尝试了所有我所了解或在此处读过的内容,但是我尝试过的任何方法都无法解决该问题.我不确定这是否有帮助,但这是我的PATH变量供参考:

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

推荐答案

我已决定通过以下解决方案来解决此问题:使用Cmd.exe或将PowerShell与Bill Stewart的Invoke-CmdScript结合使用(请参阅评论) .首先,使用Command Prompt (Cmd.exe):

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

这将激活我使用conda创建的Python 2.7环境.现在,对于Powershell版本.首先,转到此网站,然后下载Environment.ps1脚本.对我来说,它是作为文本文件下载的,因此我打开了该文件,删除了.txt扩展名,并将其替换为.ps1.因此,假设我将Environment.ps1保存在以下目录C:\Users\user_name\AppData\Local\invoke_cmdscript中.将此路径添加到环境变量"表单内的PATH用户变量中.现在,在PowerShell窗口中键入的命令:

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    

这篇关于Powershell中的Python 2和3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆