0

I have used xcode to create several python scripts. It appears in the editor fine, however, when I attempt to look at the code through the terminal, I'm seeing that new lines are being encoded as "^M". This is problematic, since I am collaborating through github, and the diff features do not work when this is being done.

E.g.: Source:

#############
#
#   test.py
#
#   by Author
#
#############

if __name__ == "__main__":
    print "This is a test"

When I save this through another editor (PyCharmer) and more it via the console, I get the output as expected. When I create a new file via xcode, past the same text, and save, I get the following:

#############^M#^M#   test.py^M#^M#   by Author^M#^M#############^Mif __name__ == "__main__":^M    print "This is a test"

Out of curiosity, I tried creating a test .cc file, and the same formatting issue did not arise, so if you want bonus points, explaining the inconsistency would be interesting as well.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
j.a.gartner
  • 197
  • 2
  • 7
  • What version of Xcode are you using, how are you creating the files, and do you have any third-party thingies to add more Python support? – abarnert Apr 27 '15 at 21:19
  • And how do you have your line endings set? See e.g. http://stackoverflow.com/q/64749/3001761 – jonrsharpe Apr 27 '15 at 21:20

1 Answers1

0

In the latest Xcode 6, if I create a new external-build-system project, set the build tool to /usr/bin/python or /usr/local/bin/python3, create a new file named test.py in that project, Xcode recognizes its type (which you can see in the File Inspector panel of the assistant editor) as "Default — Python script", and its text settings (which you can also see in the File Inspector) as:

Text Encoding: Unicode (UTF-8) Line Endings: Default — OS X / Unix (LF) Indent Using: Spaces Widths: Tab: 4 Indent: 4 Wrap lines: checked

And I get Python syntax coloring, tab completions, etc.

If that's not right—in particular, if you see Line Endings as "Classic Mac OS (CR)"—you can change it for the current file right there in the panel.

That fixes the current file. It may not fix the next file you create, but try it and see.

If not: In Xcode 4 and 5, you could easily change the default settings for each language, but that no longer seems to be exposed in Xcode 6. However, you may want to try going to the "Text Editing" pane of the "Preferences" dialog, and making sure "Default line endings" is set to "OS X / Unix (LF)", and maybe that "Convert existing files on save" is checked. This will help if you've got your default settings to Classic Mac, but C/ObjC/C++ overriding that with Unix. If, on the other hand, you somehow have a leftover override for Python from an earlier version of Xcode, I'm not sure how you can undo it short of wiping all of your Xcode settings and starting clean.

From a quick search, this answer has a very detailed version of some of the steps involved in configuring Python projects to handle things like the Run and Debug commands and so on, which may also be tangentially helpful.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671