pyenv-win / pyenv-win-venv

A CLI to manage virtual envs with pyenv-win
Apache License 2.0
78 stars 8 forks source link

Feature request: allow usage from `cmd.exe` #5

Closed bersbersbers closed 1 year ago

bersbersbers commented 1 year ago

I usually work in cmd.exe, and have to type powershell pyenv-venv all the time. What about adding a pyenv-venv.bat file that basically does powershell pyenv-venv %* or similar?

Edit: @powershell pyenv-win-venv.ps1 %* works well.

bersbersbers commented 1 year ago

(I just noticed that while most pyenv-venv things work via powershell pyenv-venv, powershell pyenv-venv init does not. Everything works when I run activate.bat myself, but then I would not need pyenv-win-venv, right?)

bersbersbers commented 1 year ago

This doesn't work too bad:

diff --git a/bin/pyenv-win-venv.ps1 b/bin/pyenv-win-venv.ps1
index 3741760..0264f06 100644
--- a/bin/pyenv-win-venv.ps1
+++ b/bin/pyenv-win-venv.ps1
@@ -32,7 +32,7 @@ function  main {
                 if (test-path "$cwd\.python-version") {
                     $env_name = (Get-Content "$cwd\.python-version")
                     if ($env_name -And (test-path -PathType container "$app_env_dir\$env_name")) {
-                        &"$app_env_dir\$env_name\Scripts\Activate.ps1"
+                        cmd /k "$app_env_dir\$env_name\Scripts\activate.bat"
                     }
                     exit
                 }
@@ -44,7 +44,7 @@ function  main {
             if (test-path $python_version_file) {
                 $env_name = (Get-Content $python_version_file)
                 if ($env_name -And (test-path -PathType container "$app_env_dir\$env_name")) {
-                    &"$app_env_dir\$env_name\Scripts\Activate.ps1"
+                    cmd /k "$app_env_dir\$env_name\Scripts\activate.bat"
                 }
             }
         }
arzkar commented 1 year ago

Sounds like a good feature. I will add a solution which ensures that the CLI will work seamlessly in CMD.

I am on vacation and don't have my laptop with me so expect an update after 9th Oct.

arzkar commented 1 year ago

powershell pyenv-venv init does not.

It seems to be working fine for me. I have this in my profile.ps1 so the python env is automatically activated whenever open a terminal in some directory containing the .python-version file.

The command init itself also works fine as well. image

bersbersbers commented 1 year ago

Sure, the automatic feature works because it is started from within a PowerShell terminal, I guess. I think my approach most closely resembles the manual activation, which I actually prefer. Does powershell pyenv-venv init work for you then, without profile.ps1? I expect it won't, or maybe I am doing something wrong.

Luckily, the solution may be simple enough by adding a one-file .bat file and switching between Activate.ps1 and activate.bat.

arzkar commented 1 year ago

Luckily, the solution may be simple enough by adding a one-file .bat file and switching between Activate.ps1 and activate.bat

I will look into it when I get some time from work. Command Prompt is pretty basic compared to PowerShell so I will need to test if everything works properly. I mostly work with PowerShell and made the CLI with powershell in mind as I am not as well versed with Cmd Prompt as I am with pwsh. Feel free to push a PR if u already have a working fix for this.

Jerakin commented 1 year ago

I also mainly work from cmd and would love if that was supported. Some environment my also have running of scripts disabled.

I made my own quick and rudimentary cmd.exe version https://github.com/Jerakin/pyenv-win-venv if I ever work on it enough for it to be "nice" I wouldn't mind putting it in this repository. If anyone is good at batch stuff I wouldn't mind some help to get it to that stage :)

arzkar commented 1 year ago

@Jerakin @bersbersbers Hey, sorry for such a late reply to this, can you check if this adds all the functionality of the CLI to CMD? Just copy all the bin files from the testing branch to your pyenv-venv installation directory's bin https://github.com/pyenv-win/pyenv-win-venv/tree/issue5_fix

bersbersbers commented 1 year ago

Hi, thanks for this. I am still using pyenv-win and that's a cool addition. I tested it for 2 minutes and it works fine at a first glance. I will test more, but I can already say I would change this to suppress extra console output:

diff --git a/bin/pyenv-venv.bat b/bin/pyenv-venv.bat
index ff573c6..7574826 100644
--- a/bin/pyenv-venv.bat
+++ b/bin/pyenv-venv.bat
@@ -1,2 +1,2 @@
 @REM Alias for pyenv-win-venv.bat
-"%USERPROFILE%\.pyenv-win-venv\bin\pyenv-win-venv.bat" %*
\ No newline at end of file
+@"%USERPROFILE%\.pyenv-win-venv\bin\pyenv-win-venv.bat" %*
\ No newline at end of file
diff --git a/bin/pyenv-win-venv.bat b/bin/pyenv-win-venv.bat
index d25e098..9f46c79 100644
--- a/bin/pyenv-win-venv.bat
+++ b/bin/pyenv-win-venv.bat
@@ -1 +1 @@
-powershell -File "%USERPROFILE%\.pyenv-win-venv\bin\pyenv-win-venv.ps1" "bat" %*
\ No newline at end of file
+@powershell -File "%USERPROFILE%\.pyenv-win-venv\bin\pyenv-win-venv.ps1" "bat" %*
\ No newline at end of file

(You can also use @ECHO OFF at the beginning of a file.)