Trying To Run A Python File [Solved]

  • Thread starter Thread starter Hornbein
  • Start date Start date
AI Thread 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,390
Reaction score
2,747
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top