As I’m preparing for a project that will involve Python programming, I need to get up to speed with at least a basic level of Python mastery. However, I’m not a big fan of using the interactive Python REPL, or whatever it is actually called:
I decided to use Xcode instead, and I’m finding it a much better solution for my needs:
Here’s the steps I took to set up this project:
Step 1: Install Python 3.5
If you run
python -V at the command line, macOS reports “Python 2.7.10”, or at least it does on my system. Bzzt. I want 3.5.2, which is the most recent non-beta release, and dates to June of this year.
I grabbed my installer from the Python.org downloads page: https://www.python.org/downloads/release/python-352/
Step 2: Locate python3
I use tcsh, so
where python3 reports /usr/local/bin/python3. The location is surely the same for you, but I don’t know what the equivalent for
where is in bash.
Step 3: Create an Xcode project
File > New > Project > Cross-platform > External Build System > Next.
Enter a name (e.g. Python), and enter the path from Step 2 into the “Build Tool” line. Click Next.
Navigate to whatever location you like, and click Create.
Step 4. Create a Python file
Choose File > New, select macOS > Other > Empty. Click Next.
You should already be in your project’s top level folder. If not, go there. Name your file Whatever.py, choosing whatever name you like. I went with Work.py. Make sure the “add to target Python” box is checked. Click Create.
Step 5. Edit your Run Scheme
The Xcode default should have the Run scheme selected:
Click and hold on the Python target in the jump bar. Select Edit Scheme…
The Run scheme displays, with the Info tab selected.
Step 6. Choose the Executable
I warn you now that this step is going to be delicate, fragile, and stupid. That’s because Xcode, for whatever reason, will not let you use the symbolic link at /usr/local/bin/python3. I don’t know why.
In the Info tab. Select “Other” from the Executable pop-up list. A file selection dialog appears.
Return to the terminal. Type:
open /usr/local/bin. Select
python3 and control-click/right-click. Select Show Original. This will probably be named python3.5. It’s not a symbolic link but unfortunately Xcode continues to be fussy about allowing you to select it as your executable because of the period in its name. Sigh.
python3.5 onto the file dialog and click Choose, if you’re allowed to. If so, great. If not, you need to work around Xcode: create a hard link and then drag the link onto the dialog.
% ln python3.5 python35
I know, I know. Ew. But it’s better than copying, or worse, renaming the file. And no, symbolic links don’t seem to work here. Better solution? Let me know.
Finally, uncheck “Debug executable”. You don’t want to debug the Python language itself.
Step 7. Add Launch Arguments
Now, click the Arguments tab. Click + under “Arguments Passed On Launch” and type
$(SRCROOT)/ followed by the name of the Python file you created in Step 4.
Step 8. Test it out.
Click Close to dismiss the scheme editor. Enter a program (don’t forget all those colons and tabs) and run it:
It’s a very odd thing to be jumping into Python with a Swift background. Clearly Swift has inherited a lot of Python genes. It also feels sinful to use such lax typing without compiler oversight. That said, my first experiences in Python can wait for another day and another post. More to follow.