Python Forum

Full Version: Why can I not see the code correctly in Python IDLE.?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using Python IDLE 2.7.18.4 on Windows 10. I copied a code from a pdf in my editor and made it as comments. The code look like this in my editor:
[Image: view?usp=sharing]
but when I past the code here (in this forum), I see it correctly as I see some parts of the code are not as comments and thus I got an error:


##import arcpy
##infc = arcpy.GetParameterAsText(0)
##
outfc = arcpy.GetParameterAsText(1)
##
arcpy.Copy_management(infc, outfc)
and I get this error:

Traceback (most recent call last):
File "D:/ArcPy_Ex/test.py", line 5, in <module>
outfc = arcpy.GetParameterAsText(1)
NameError: name 'arcpy' is not defined


I wonder How I can see the code correctly in the Python IDLE editor?
You are getting an error because of line 4, not because of anything in the comments. If you want to do this:
outfc = arcpy.GetParameterAsText(1)
You need to first do this:
import arcpy
You should not use Python 2.7, use a current version of python.
I posted something here but i want to delete it. Can I delete a post here?how?
# is the start of a line comment in Python. Everything following # in the line is a comment. Don't depend on syntax highlighting.

The error message told you where the error happened.
Error:
Traceback (most recent call last): File "D:/ArcPy_Ex/test.py", line 5, in <module> outfc = arcpy.GetParameterAsText(1) NameError: name 'arcpy' is not defined
Note the lack of a comment character in outfc = arcpy.GetParameterAsText(1)
(Mar-11-2024, 12:21 AM)deanhystad Wrote: [ -> ]# is the start of a line comment in Python. Everything following # in the line is a comment. Don't depend on syntax highlighting.

The error message told you where the error happened.
Error:
Traceback (most recent call last): File "D:/ArcPy_Ex/test.py", line 5, in <module> outfc = arcpy.GetParameterAsText(1) NameError: name 'arcpy' is not defined
Note the lack of a comment character in outfc = arcpy.GetParameterAsText(1)

Thanks for the reply. As I posted later (the image I posted, can it be seen? ), it is seen differently and I woder, now, how I can see the code in the Python IDLE like it is seen here?
Access denied on viewing the image.
(Mar-11-2024, 05:16 AM)deanhystad Wrote: [ -> ]Access denied on viewing the image.

Sorry about it. This should work, hopefully:
[Image: KNL6Pjp]
How do you run your program?

I don't know if the img tags work. Look here for instructions on how to attach a file.

https://python-forum.io/misc.php?action=...ight=image
(Mar-11-2024, 12:21 AM)deanhystad Wrote: [ -> ]# is the start of a line comment in Python. Everything following # in the line is a comment. Don't depend on syntax highlighting.

The error message told you where the error happened.
Error:
Traceback (most recent call last): File "D:/ArcPy_Ex/test.py", line 5, in <module> outfc = arcpy.GetParameterAsText(1) NameError: name 'arcpy' is not defined
Note the lack of a comment character in outfc = arcpy.GetParameterAsText(1)

That right. To resolve this problem and see the code correctly in the Python IDLE editor, you need to ensure that the comments are properly formatted with a single '#' symbol at the beginning of each comment line.
«spam link removed»