Macs in Chemistry

Insanely great science

 

Python coding within Xcode

I was reading a recent KDnuggets article on a recent poll "What Python IDE / Editor you used the most in 2020?", as expected the poll was topped by Jupyter Notebook (42%), JupyterLab added extra (14%). Visual Studio Code, PyCharm and Spyder were also popular options.

I started wondering if it was possible to use Xcode to code python, the answers "Yes", but it requires a little setting up to do. After a fair bit of online searching I managed to put together a set of instructions that I thought I'd share.

I'm using Anaconda so the first thing is to get the path to Python. Open up a Terminal window and type

which python
/users/username/miniconda3/bin/python

This gives the path to the currently used python installation, if you are not using Anaconda it might be /usr/local/bin/python3 we can also get the python version.

python -V
Python 3.7.6

Now open Xcode and choose "Create a new Xcode project", in the resulting dialog select the "Other" tab and then "External Build System".

template

Click "Next" and then enter your python project name and check the build tool is pointing to the correct python installation. Click "Next" and navigate to where you want to create the project file.

Project

Now create a python file (choose "New" from the "File" menu, scroll down to the bottom of the selections in the dialog to "Other" and then select "Empty". Click "Next" and save this in the Python project folder you created.

ChooseFile

Name the file and remember to add the .py file extension.

SaveFile

The next step is to edit the run scheme, click on the target symbol as shown below and select "Edit Scheme" from the dropdown menu.

EditScheme

In the "Info" tab select "other" from the dropdown menu and a dialog box will open and you need to navigate to the python executable you identified in the Terminal above

linktopythonexecutable

Ideally you would simply select python in the dialog as shown below, this is a symbolic link to python3.7 however this does not seem to work. So we need to create a hard link. In the Terminal type

ln python3.7 python37

And then choose python37

pythonlink

Now uncheck debug executable, since we don't plan to debug python.

debugpython

Now click on the "Arguments" Tab and add the name of the python file.

pythonArguments

Now click on the Options tab, click the Use custom working directory check box and then click on the folder icon to navigate to the folder containing the python project. Now close the scheme editor.

options

Writing some Python Code

Below is a very simple piece of python that takes a Kekule formatted SMILES string and uses RDKit to convert it to the aromatic form, the output is shown in the box bottom right.

pythonscript

So it works, but I can't help but think it should be a little easier to set up.

Last Updated 19 October 2020