Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Create PowerShell shortcut similar to "WinPython Command Prompt.exe" #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dougthor42 opened this issue Nov 23, 2015 · 15 comments
Closed

Comments

@dougthor42
Copy link
Contributor

Summary:

Create a shortcut to a PowerShell session with all of the environment variables set already.

Reasoning:

Windows PowerShell is a more powerful, more feature-rich, and more customizable shell than the command prompt, cmd.exe (especially in Windows 10 which added even more goodies).

Implementation Issues:

The only issue I can think of right now is that, by default, Windows restricts powershell scripts from running.

  • Option 1: Run Set-ExecutionPolicy RemoteSigned or Set-ExecutionPolicy Unrestricted in powershell before running the script. This, however, requires administrator privileges which (in my opinion) goes against one of the primary benefits of WinPython.
  • Option 2: You can run the powershell script via powershell -ExecutionPolicy ByPass -File '.\WinPython Command Prompt (PowerShell).ps1'. But in order to do that you must already have a PS window open. Not a big deal, but it means that it's a 2-step process.
@stonebig
Copy link
Contributor

That is the problem of PowerShell: if you create an user application requiring PowerShell, it will be non-executable to 95% of the intended users.

I'm interesting myself in the xonsh idea since a few days, as a possible workaround path forward.

What do you think of xonsh ?

@dougthor42
Copy link
Contributor Author

Yeah, PowerShell can be kinda lame for non-admins.

I've never heard of xonsh, but I did just look it up. Looks kinda interesting, but I doubt that I'd use it. I'd say it would be a good addition, but I wouldn't replace CMD with it.

@stonebig
Copy link
Contributor

stonebig commented Jan 3, 2016

to reconsider when Windows users will be 50% on Windows 10, this summer 2016

@stonebig stonebig added this to the 2016-05 Summer Pack / Jupyterlab milestone Feb 6, 2016
@stonebig
Copy link
Contributor

stonebig commented Feb 6, 2016

Let suppose:

  • everybody is Windows 10,
  • Jupyter 5 is at 5.1, and xonsh / python-prompt friendly
  • some required miracle occures (We saw unprobable things recently).

Then:

  • include xonsh
  • include a powershell start
  • include a pythonnet module

@stonebig stonebig modified the milestones: 2016-07 next big things, 2016-05 Summer Pack / Jupyterlab Feb 6, 2016
@stonebig stonebig modified the milestones: 2016-04 Summer Pack / Jupyterlab, 2016-06 the next big (data ?) thing Feb 14, 2016
@stonebig stonebig modified the milestones: 2016-04 Matplotlib 2.1 / Spyder 3.0 / Jupyter 5, 2016-03 Matplotlib 2.0 / Spyder 3.0b4 / mingwpy update Apr 5, 2016
@stonebig
Copy link
Contributor

@dougthor42 what should contain WinPython Command Prompt (PowerShell).ps1 ?

@dougthor42
Copy link
Contributor Author

@stonebig Can you elaborate? I'm not sure I understand the question. What do you mean by "what should contain"?

@stonebig
Copy link
Contributor

stonebig commented May 27, 2016

well, let's take a current ".bat" launcher, "winpython.bat":

@echo off
call "%~dp0env_for_icons.bat"
cd/D "%WINPYWORKDIR%"
rem backward compatibility for non-ptpython users
if exist "%WINPYDIR%\scripts\ptpython.exe" (
    "%WINPYDIR%\scripts\ptpython.exe" %*
) else (
    "%WINPYDIR%\python.exe"  %*
)

What should I modify to get a powershell one ? (trying to experiment how to do your whish)

@dougthor42
Copy link
Contributor Author

Ah, ok.

So I think we may be talking about two different things. Please let me know if that's not the case.

  1. Opening the python interpreter (>>>) directly, like is done with WinPython Interpreter.exe or your winpython.bat
  2. Opening a prompt that has the environment variables set so that python can be run via just python.

It sounds like you're talking about #1 being opened inside a PS window, while my original request was for #2 opening in a PS window.

1. Opening the Python interpreter directly inside a PS window.

I don't actually know how to do this one. I also don't think it's needed because once you're in the Python interpreter, it doesn't matter if your using Windows CMD, PS, Bash, or any other shell environment. Exiting the Python interpreter also closes your shell window.

2. Opening a PS prompt that has all the environment variables set.

I was messing with that a while ago, back when I first suggested the feature. I came up with the following.

  1. Create a PowerShell script to set all the environment variables. This is basically did the same thing as scripts/env.bat back in WinPython 2.7.6.2. Note that this script has not been fully tested. It should be updated to match whatever is done in the latest version of scripts/env.bat.

    ###############################
    ### WinPython_PS_Prompt.ps1 ###
    ###############################
    
    $env:WINPYDIR = "$pwd\python-2.7.6"
    $env:WINPYVER = "2.7.6.2"
    $env:WINPYARCH = "x64"
    $env:HOME = "$env:WINPYDIR\..\settings"
    
    # Set the Window Title
    $host.ui.rawui.WindowTitle = "WinPython $env:WINPYVER $env:WINPYARCH PowerShell Prompt"
    
    $env:path = "$env:path;$env:WINPYDIR"
    $env:path = "$env:path;$env:WINPYDIR\Lib\site-packages\PyQt4"
    $env:path = "$env:path;$env:WINPYDIR\"
    $env:path = "$env:path;$env:WINPYDIR\DLLs"
    $env:path = "$env:path;$env:WINPYDIR\Scripts"
    $env:path = "$env:path;$env:WINPYDIR\..\tools"
    $env:path = "$env:path;$env:WINPYDIR\..\tools\gnuwin32\bin"
    $env:path = "$env:path;$env:WINPYDIR\..\tools\mingw32\bin"
    $env:path = "$env:path;$env:WINPYDIR\..\tools\TortoiseHg"
  2. And also create a batch script so that the user can double-click on it to run. It also sets the execution policy of the PS window (and only that PS window) to allow the PS script to run.

    rem WinPython_Interpreter_PS.bat
    Powershell.exe -executionpolicy RemoteSigned -noexit -file "WinPython_PS_Prompt.ps1"

    (note that colors will be a little off. See this SO answer to change colors, but it won't be perfect).

@stonebig
Copy link
Contributor

stonebig commented May 27, 2016

Thanks! It should be enough for me to try connecting the dots:

  • include proposed script in WinPython Generation
  • make a bi-mode WinPython_PS_Prompt.ps1 (initialize $env:WINPYDIR if not done only)
  • add a Powershell launcher based on env.bat initialization

@stonebig
Copy link
Contributor

stonebig commented May 29, 2016

I will add this, a true value-addition for me:

###############################
### Set-WindowSize
###############################
Function Set-WindowSize {
Param([int]$x=$host.ui.rawui.windowsize.width,
      [int]$y=$host.ui.rawui.windowsize.heigth,
      [int]$buffer=$host.UI.RawUI.BufferSize.heigth)

    $buffersize = new-object System.Management.Automation.Host.Size($x,$buffer)
    $host.UI.RawUI.BufferSize = $buffersize
    $size = New-Object System.Management.Automation.Host.Size($x,$y)
    $host.ui.rawui.WindowSize = $size   
}

Set-WindowSize 150 40 6000 

#338

@stonebig
Copy link
Contributor

Well, my Powershell1 comprehension is far from perfect, so for now:

  • the Powershell shortcut will launch the classic DOS initialization,
  • then your tweaked script.

@stonebig
Copy link
Contributor

Powershell Noob question: should I duplicate $env:HOME value to $HOME ? (and so on for $env:WINPYDIR ... etc ...)

@stonebig
Copy link
Contributor

merged in beta3 with #339

@dougthor42
Copy link
Contributor Author

Sorry for the delayed response.

should I duplicate $env:HOME value to $HOME

I'm not much of a PS guru either, but my initial thought is no, you don't need to duplicate them.

value-addition

I agree! Default CMD/PS sizes are far too small...

@warren-bank
Copy link

warren-bank commented Jan 21, 2019

sorry to bump an old thread.. especially since this comment is fairly obvious.. but it's probably worth mentioning..

for those of us who

  • dislike PowerShell
  • like bash
  • use git for windows.. and already have a bash shell available

it's super easy to run python, pip, etc from bash..

  • make a new file python-bash.cmd in a directory that's in PATH
  • configure to match how you organize your directories:
      @echo off
    
      set winpython_home=C:\PortableApps\WinPythonZero\3.7.1.0
      set portablegit_home=C:\PortableApps\git\2.16.2\PortableGit
    
      call "%winpython_home%\scripts\env.bat"
    
      start "bash" "%portablegit_home%\git-bash.exe" %*
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants