Trying To Run A Python File [Solved]

  • Context: Python 
  • Thread starter Thread starter Hornbein
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
Hornbein
Gold Member
Messages
4,112
Reaction score
3,274
I installed anaconda3, but the Spyder program that is supposed to run Python fails by going into an infinite loop of "Restarting Kernel." I can however run Python standalone and get a prompt. >>>

Now how to run a file. Here's what happens when I try running from the Python command prompt.

>>> exec(open("c:\users\asus\pf.py").read())
File "<stdin>", line 1
exec(open("c:\users\asus\pf.py").read())
^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape
>>>
I copied that line from Notepad after retyping the line by hand to make sure it had no hidden characters. Same result.


I also tried running from the Windows command prompt. What I got there was "python not found."
 
on Phys.org
Its the backslashes in your path. Either use a raw string (i.e. r'c:\users\asus\pf.py') or replace all backslashes with forward slashes (i.e. 'c:/users/asus/pf.py'). You can technically also escape each backslash with a backslash (i.e. 'c:\\users\\asus\\pf.py') but that is not very pretty or maintainable.
 
Last edited:
Filip Larsen said:
Its the backslashes in your path. Either use a raw string (i.e. r'c:\users\asus\pf.py') or replace all backslashes with forward slashes (i.e. 'c:/users/asus/pf.py'). You can technically also escape each backslash with a backslash (i.e. 'c:\\users\\asus\\pf.py') that is not very pretty or maintainable.
Bingo.
 
Reply
  • Like
Likes   Reactions: berkeman