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

17
  • 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. Commented Mar 7, 2023 at 15:39
  • Oops sorry, For 1) it's only a copy-paste mistake. I've updated the config. Commented Mar 7, 2023 at 15:42
  • 1
    @shadowtalker strings in powershell can be 'single quoted' or "double quoted". Only cmd doesn't allow single quotes Commented Mar 7, 2023 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. Commented Mar 7, 2023 at 15:49
  • 1
    :( Third time lucky? & "$VENV_DIR\Scripts\python.exe" --version Commented Mar 7, 2023 at 16:55

1 Answer 1

0

Have you tried putting an ampersand before the python command?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.