2

I have this enviroment variable: VAR=C:\Users\User but how can I prevent python giving this "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error when I try:

import os
os.environ["VAR"]

without needing to change the variable in Command line

Matthijs990
  • 575
  • 2
  • 24

3 Answers3

3

Add r to let compiler knows it's a raw string.

r'{}'.format(os.environ["VAR"]))

However, your code works on myside without any changes.

enter image description here

Raymond
  • 1,300
  • 10
  • 26
0

The unicode error comes from the "\U". So you can also change your

VAR=C:\Users\User

to

VAR=C:\\Users\\User

so that Python recognizes the dashes as literal dashes.

jeppoo1
  • 534
  • 8
  • 17
-1

Maybe the reason are the dashs. Did you try:

Try VAR=C:/Users/User
Lazloo Xp
  • 744
  • 1
  • 7
  • 26