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
  • 637
  • 3
  • 26

3 Answers3

2

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

Ray
  • 1,539
  • 1
  • 14
  • 27
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
  • 650
  • 1
  • 10
  • 23
-1

Maybe the reason are the dashs. Did you try:

Try VAR=C:/Users/User
Lazloo Xp
  • 858
  • 1
  • 11
  • 36