1

I'm facing a basic and stupid problem on Windows PowerShell. I'm almost sure it has already been answered somewhere but I can't find something working for me.

I simply would like to use a variable inside a command in PowerShell:

$VENV_DIR='C:\venv\'
python -m venv $VENV_DIR
$VENV_DIR\Scripts\python.exe --version

I expect to see Python 3.10.8 as result.

But I have this error:

 PS C:\> $VENV_DIR\Scripts\python.exe --version
At line:1 char:10
+ $VENV_DIR\Scripts\python.exe --version
+          ~~~~~~~~~~~~~~~~~~~
Unexpected token '\Scripts\python.exe' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErr
   orRecordException
    + FullyQualifiedErrorId : UnexpectedToken

I tried a lot of different combinations, but none of them work

  97 $VENV_DIR\Scripts\python.exe --version
  98 `$VENV_DIR\Scripts\python.exe --version
  99 $VENV_DIR\\Scripts\python.exe --version
 100 $VENV_DIR\Scripts\python.exe --version
 101 "$VENV_DIR"\Scripts\python.exe --version
 102 "$VENV_DIR\Scripts\python.exe" --version
 103 "$VENV_DIR\Scripts\python.exe --version"
 104 ("$VENV_DIR\Scripts\python.exe --version")
 105 $("$VENV_DIR\Scripts\python.exe --version")
 106 -$("$VENV_DIR\Scripts\python.exe --version")
 107 -$("$VENV_DIR")\Scripts\python.exe --version
 108 -$($VENV_DIR)\Scripts\python.exe --version
 109 $VENV_DIR\Scripts\python.exe --version
 110 (echo $VENV_DIR)\Scripts\python.exe --version
 111 echo $VENV_DIR\Scripts\python.exe --version
 112 $(echo $VENV_DIR)\Scripts\python.exe --version
 113 -$(echo $VENV_DIR)\Scripts\python.exe --version
 114 -$("echo $VENV_DIR")\Scripts\python.exe --version
 115 echo $VENV_DIR\Scripts\python.exe --version

Could you please help? Thanks

iAmoric
  • 1,787
  • 3
  • 31
  • 64
  • 1) you forgot a `'` at the end, 2) aren't strings in Powershell supposed to be double-quoted anyway? This might just be a Powershell syntax error. – shadowtalker Mar 07 '23 at 15:39
  • Oops sorry, For 1) it's only a copy-paste mistake. I've updated the config. – iAmoric Mar 07 '23 at 15:42
  • 1
    @shadowtalker strings in powershell can be `'single quoted'` or `"double quoted"`. Only cmd doesn't allow single quotes – phuclv Mar 07 '23 at 15:44
  • 1
    @shadowtalker both single-quoted and double-quoted strings are valid, with different semantics. The difference is that `$` variables get expanded in a double-quoted string and not in a single-quoted string. – slothrop Mar 07 '23 at 15:49
  • Thanks, but this is not really answering my question :( See the tests I did. I tried with `"` and `$` – iAmoric Mar 07 '23 at 15:52
  • Is it the doubling of the backslash? `$VENV_DIR\Scripts` evaluates to `C:\venv\\Scripts` not `C:\venv\Scripts` – slothrop Mar 07 '23 at 16:02
  • @slothrop It was a good idea but still the error :/ – iAmoric Mar 07 '23 at 16:08
  • Sorry :( How about `& "$VENV_DIR\Scripts\python.exe --version"` ? – slothrop Mar 07 '23 at 16:12
  • Still not working. I also tried this: https://stackoverflow.com/questions/40915420/how-to-expand-variable-in-powershell without success – iAmoric Mar 07 '23 at 16:50
  • 1
    :( Third time lucky? `& "$VENV_DIR\Scripts\python.exe" --version` – slothrop Mar 07 '23 at 16:55
  • Ok, this time it's better. At least powershell translates the variable. But still not working because of: ` The term 'C:\venv\Scripts\python.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.` – iAmoric Mar 07 '23 at 17:06
  • Does that file exist in that directory? – slothrop Mar 07 '23 at 17:11
  • Wouldn't you rather expect python to exist at `$VENV_DIR\bin\python.exe`? ` – Mathias R. Jessen Mar 07 '23 at 17:12
  • Without the variable it works: ``` C:\> C:\venv\Scripts\python.exe --version Python 3.10.8``` – iAmoric Mar 07 '23 at 17:14
  • OK, sorry, I can't reproduce this and I'm stuck - the command with `&` and a variable works fine on my setup (admittedly not with identical directory names to yours) – slothrop Mar 07 '23 at 17:56
  • Finally solved. The problem came from the way I declared the variable: `$VENV_DIR='C:\venv'` --> `$VENV_DIR="C:\venv"` (double quote) – iAmoric Mar 08 '23 at 08:59
  • Interesting, for me it works with either single or double quotes - weird! But great you could get it fixed! – slothrop Mar 08 '23 at 09:55

1 Answers1

0

Have you tried putting an ampersand before the python command?

Walter Mitty
  • 18,205
  • 2
  • 28
  • 58