In this video, I’ll demonstrate how you can use Xcode 5 to build and run Python scripts.
Note: There is an update to this article, posted on the 11th of July 2013. You can find this article here: Build and Run Python Scripts Using Xcode 5
So you have installed Python and you want to use Xcode to write and run your Python scripts… is that correct? Ok then, follow these steps to create a simple project and run it with Python using Xcode 4.x…
- Open Xcode and select File->New->Project…
- From the left hand side, under the OS X category, select Other
- Now on the right hand side, select External Build System and press the Next button, as shown below
- Now open a Terminal window and type which python in it, and keep the results in mind or just keep Terminal open for future reference, as shown below. This will tell you where Python is installed on your system.
- As you can see, in my case Python is installed under /usr/bin/Python
- Now back to Xcode, set the following values on the second screen of project creation:
- Product Name: test
- Organization Name: company
- Company Identifier: com.company
- Build Tool: enter the path to Python which we retrieved from Terminal. I have entered /usr/bin/Python (see below)
- Once you are done entering all four values, press the Next button.
- Once you pressed Next, now you should choose where you want to save your project, save it somewhere that makes sense for you. I am going to save it in ~/Desktop/Development/Projects/Python/Sandbox/test/ as that’s how I have structured my development environment. Once you are happy with the folder that you are saving your project in, press the Create button in Xcode’s dialog, as shown below
- Now select the “test” target from the breadcrumb menu on top left corner of Xcode’s main window and choose Edit Scheme, as shown below
- In the Edit Scheme screen, on the left hand side, choose the Run option. Then on the right hand side, choose the Executable section and press the Other… item. This will open an Open dialog for you asking you to choose the binary of Python. For me, Python was sitting in /user/bin/python as you saw earlier. So in Mac, I press Cmd+Shift+G to “Go to Folder” and then I type in /usr/bin/, as shown below and then press Go:
- Now that you are in the folder that contains the Python binary, find and pick the binary and press the Choose button
- Back in Xcode, on the same Scheme window that we were on before, under the Debugger section under Info, make sure that you choose None. This is very important.
- Back in Xcode, on the same Scheme window that we were on before, navigate on the right hand side to the Arguments tab and under the Arguments Passed On Launch, press the + button and type test.py, as shown below:
- Now go to the Options tab, under the Working Directory, click the Use custom working directory and then put the full path to where you are intending to save your Python files in this project. My project is at ~/Desktop/Development/Projects/Python/Sandbox/test/ and I’m intending to save my first file as ~/Desktop/Development/Projects/Python/Sandbox/test/test.py so I will choose ~/Desktop/Development/Projects/Python/Sandbox/test/ as my working directory, as shown below:
- We are now done with the Schemes. Press the OK button to close this window.\
- In Xcode, from the Menus, choose File->New->File…
- From the left hand side of the dialog, under the OS X section, choose Other and then on the right hand side, choose Empty and press the Next button, as shown below:
- In the next screen, make sure that you set the value of Save As as test.py. This is what we have been preparing our Scheme for, that we are going to have a file called test.py that needs to be passed to the Python interpreter. Have a look at the following screenshot. Once you are done, press the Create button:
- Now enter the following code in your test.py file: print “It works!”, as shown below:
- As the final step to make sure everything is working fine, in Xcode, from the menus, choose Product->Run, the Debugging Area will popup and show you the output of the print command like so:
Lovely, it’s all working. I hope this tutorial helped you. If you have any questions or suggestions, please leave them in the comment sections down below…
I wanted to copy some files from my computer over to a server every minute, as a cronjob. I searched around the internet but the solutions were scattered all over the place and a lot of “do this and do that” so I decided to put things together in this comprehensive video.
In this video, you will learn:
- How to set up your RSA private/public keys.
- How to set up passwordless SSH access to your server.
- How to “scp” files from your client to your server under your user name.
- How to set up a cronjob to copy your files over to the server once every minute.
Xcode 4.3.x or newer comes with Git but the problem is when you install Xcode on your machine (OS X Lion or newer), Git’s path won’t be added to the user path which means if you run git from your command-line, your system will say:
-bash: git: command not found
Xcode’s installation of Git is at the following location on your machine:
With the git binary sitting here:
To add this binary to your path (which will allow you to run “git” from any directory on your system), go to terminal and type this command:
There is one caveat to this approach and that is the PATH will only be changed in your current running instance of terminal. As soon as you close terminal and open it again, you will have to enter the above command again to get access to the git app. So what is the proper solution? You will have to add the above “export” command to the .profile file in your home directory. The .profile file gets read every time you open terminal. So open a terminal instance and type the following command:
And then type this command:
ls -la | grep “.profile”
We are trying to find out if we already have a file named .profile in our home directory. If after running the above command you won’t see anything getting printed to the terminal, use the following command to create a new .profile file. If you already have a .profile file, skip this command:
Now open the .profile with this command:
Now add the git path to the PATH variable in the .profile file so that your .profile content will look something like this (it really depends on what you already have in this file. I am assuming your .profile file didn’t exist until now and you just created it):
Save your changes to the .profile file and close terminal and open it again. Now whichever directory you are in, in terminal, you can use the git command. Good luck.
In this tutorial you will learn:
- Downloading DocBook Documentation
- Writing DocBook in Xcode
- Validating DocBook XMLs
- Generating PDF from DocBook XMLs
To download MacPorts, go to: http://macports.org/
To install XMLLINT categories, use this Terminal command:
sudo port install docbook-xml xmlcatmgr
To validate a DocBook XML, place these commands (as demonstrated in the video) into an executable file and run by passing the name of the file to validate as an argument:
xmllint –valid –noout –catalogs $fileToValidate
Questions? let me know and watch for the next videos about DocBook