Trying To Run A Python File [Solved]

  • Thread starter Thread starter Hornbein
  • Start date Start date
Click For Summary
The discussion centers on issues with running Python scripts in Anaconda's Spyder, where users experience an infinite loop of "Restarting Kernel." While standalone Python runs without issues, attempts to execute a script using the command prompt result in a SyntaxError due to improper path formatting. The error arises from using backslashes in file paths, which can be resolved by either using a raw string (e.g., r'c:\users\asus\pf.py'), replacing backslashes with forward slashes (e.g., 'c:/users/asus/pf.py'), or escaping backslashes (e.g., 'c:\\users\\asus\\pf.py'). The discussion concludes with a successful resolution by correcting the path format.
Hornbein
Gold Member
Messages
3,563
Reaction score
2,940
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."
 
Technology news 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 27 ·
Replies
27
Views
5K
  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
15
Views
9K