Search code examples
pythonxcode

How can I run Pygame (and python in general) in xcode?


I want to put a simple python game in the IOS app store but I am struggling to get it work properly in xcode, which prevents me from easily publishing the app.

I have looked online and I am able to open the python in xcode, I just can't get it to run. I am not sure it is possible, but I have seen some older forum that suggest it is.

I have tried to open and run the python code in xcode, and I assumed it would run similar to swiftUI which is more purpose built but it did not work as expected.


Solution

    • make sure you have the latest versions of python (use brew) and xcode.
    • find the location of your python install using where python3.
    • run the commands:
    cd location_of_python3
    ln python3 examplePython3
    
    • create an external build system
      • go to File->New->Project->Other->External Build System external build system
      • in the next prompt set the product name (doesn't matter what)
      • in the build tool section, set the path of the where command prompt
      • now you can see your project's info screen info screen
    • make a new python file
      • File->New->File->macOS->Empty-> click choose
      • choose a name for your python file (.py included)
    • scheme editing
      • click the target
      • click Edit Scheme
      • click Executable -> Other (this will open a finder)
      • press CMD + SHIFT + G
      • enter the path from the where command
      • click Go
      • search for the python3 linked file created in the third step
      • choose the home file
    • add launch arguments
      • next to the info section, you can also see the Arguments section.
      • click that
      • In Arguments Passed On Launch, click the + button and give the python filename as input.
      • press save
    • set the working directory
      • next to the Arguments tab, you can see a Options tab.
      • click that
      • use a custom working directory
      • set the path as the project path
    • debug the executable
      • go to the Info tab
      • uncheck the Debug executable option
    • test
      • open your python file via xcode
      • write some python code
      • click Run and see the output!

    If you have any other questions, just ask.