简体   繁体   中英

python raises syntax error in environment variable path

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

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.

在此处输入图片说明

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.

Maybe the reason are the dashs. Did you try:

Try VAR=C:/Users/User

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM