Connecting Python 3.8 with Xcode 11

Hi. I have some trouble while on connecting latest Python 3.8 on the Xcode 11. I have been fighting with the problem almost couple of days, and I finally concluded I must get help from the masters. I'm new to Xcode and other programming stuff, please forgive me on being clumsy;)


I first type on google Xcode, Python and read some posts about bringing Python on the Xcode. Though they're bit outdated (mostly on 2016, 2017) but still the format was similar, so I decided to follow them step by step.


First, open the Xcode and create the new Project. Add product name 'pythons', and fill other blank below. On Build Tool, I pasted


/Library/Frameworks/Python.framework/Versions/3.8/bin/python3


which is the address of Python3 I found by typing 'which python3' on Terminal. Than I pressed Next, and create project folder on Desktop, checked on options: 'Source Control: Create Git repository on my Mac'.


Secondly, create the new file. I went to 'File-New-File' at the menu and found 'Empty' template at 'macOS-Other' category. Click Next, save as

File.py, checked on 'Group: pythons, Targets: pythons'. And created the file at the folder 'pythons'.


Third, edit scheme on the created file. I brought scheme edit page by 'Product-Scheme-Edit Scheme'. At the Info tab, I changed 'Exexutable' on 'other' choosing python38, which I made it newly by opening the Terminal on the '~bin' folder which I mentioned on Build Tool, and typing on Terminal


ln python3.8 python38


By the procedure, I could choose python38 instead of python3, which was non-selectable with gray shade on it. Than I un-checked the 'Debug executable'.

Move to Arguments tab and click + of 'Arguments Passed On Launch'. Than I typed in $(SRCROOT)/File.py

Move to Options tab, there is Working Directory option, and I checked on 'Use custom working directory' to the project path on Desktop.


Finally, close the scheme editing page and write 'print("Hello world")' on the canvas. Run it.


And there are errors, which I couldn't solve out.


/Library/Frameworks/Python.framework/Versions/3.8/bin/python38: can't open file '$(SRCROOT)/File.py': [Errno 2] No such file or directory

Program ended with exit code: 2


How can I solve this errors and get back on working Python with Xcode?


Many people on the net say that build python with other tools such as pycharm, IDLE or other stuff, I guess I should. But I really want to know at least what was worng with the steps I took and just wanna see 'Hello world' on the output.


Please help me.. This problem keeps tangling like an fishbone on my neck every time. Welcomes precise feedbacks. Thanks.

  • Just provide the actual path to the file e.g. /Users/...Path/To/The/File

Add a Comment

Replies

Can you explain what you are trying to do? People don't normally use Python with Xcode at all. People who do use Python on the Mac typically stick to the system version. You can download stand-alone installers for the latest version of Python from the Python web site. Is there a reason why you aren't doing that?

Got this from github and seems to work.



How to use Xcode IDE for Python development

Open Xcode and start with creating a new project: From the templates, choose Cross-platform > External Build System Give it a name. You can update Organizatin Name and Identifier if you want. Make sure you enter a correct path for Python bin. For Python 3.x it's usually

/usr/bin/pythonw
. Click Next and save it. Open scheme editor, select Run from the left hand pane, and Info tab from the detail pane. From Executable menu, select Other... and navigate to where your Python executable binary is locate. That's the same location you entered in step 4, e.g.
/usr/bin/pythonw
. You can also use **** + Command + G and paste the address in the dialog box. Uncheck Debug executable box. Still in Run menu, select Arguments tap, and add a new entry to Arguments Passed on Launch. This is the name of the file where you'll write your Python code in it. You'll create the file later. Still in Run menu, select Options tab, and check the box for Use custom working directory. Enter the path to the directory where you have saved your project. Close scheme editor. In Xcode navigation pane, under your project, add a new file. Select Empty from the templates. Make sure the target is check and that you save the file in the same directory where the project file is located. Now you can start writing Python code:

Run it!



https://github.com/moayes/Xcode-python

Hi,

I had the same problem and the issue was that SRCROOT is an environment variable and it is or was not set. Either create the environment variable or point to the absolute location of the file you have created in the scheme.

that fixed for me.

Add a Comment

Change "Expand Variables Based On = "Python" (my xcode file name)" in Arguments panel

Sigh. I have the same problem; tried using XCode as my developement environment for my master's class. Too many of the repliers fail to realize that python 3 is installed in a different place (library/frameworks/...) now than the older usr/bin location for 2.7. And that's by the official installer from the python org website.


I tried doing it by Homebrew so it would go in the 'normal' place, but Homebrew would only install an older version of python3 versus the listed current version, so I dumped it all (My class was based on the lastest version of python, so I didn't want to run into code incompatibility issues).


Frustrated with the entire ordeal, I scrapped it. Switched back and forth between using BBEdit for editing the python scripts or the basic IDLE environment included with the app (BBEdit can't run interactive programs requiring user input, but it's fine for simple scripts).


it's a shame none of these 'master's' have a better answer.

I am no master, but removing $SCROOT/ from the argument section helped me. I also checked the working directory box in the options section and setting the custom directory to the project folder. This solved the problem for me.

@Barnstormr you can use many version of python : just specify the binary in the good directory in the first line of your code like this :

#!/usr/local/bin/python3.6
HI!
I had the same problem. You may simpe type full path in as argument /Users/*user*/...../test/test.py

Trying to get rid of passing arguments for each new file created in order to run that specific file, if multiple files are there. (Horrible title, i know. Please bare with my english)


Okay so, i was also trying the same, running python3.8 with Xcode12 and with the same method as you have stated. I got to know that in 'Arguments' tab when stating $(SRCROOT)/<your filename>.py, the Xcode (or the system, whatever) is not interpreting it as a command, its just simply passing it as a string (unfortunately). So, in the 'Options' tab i checked the option for Use custom working directory and specified the project folder where my actual python code file was and simply specifying the filename in the 'Arguments' tab. you got the point, right ?

Now, before this, what i was trying to do, is to pass $(SRCROOT)/$(ls -1t | head -1) to the arguments so that, i no need to pass the file name in arguments for each time i create a new python code file. For example: If i created program1.py then, i no need to specify this in arguments as $(ls -1t | head -1) will output the most recently modified file. So, even if i have created more than 1 file i just need to trigger a very minor change and then save it so that it became the most recent modified file, like adding space at the very end or pressing return key to add a new line, as this will make a effect of file being modified but no change to the program logic. I hope you people get it what i am trying to achieve here.

But unfortunately, it is taking the arguments passed as string and not being interpreted as a command. Please, do help here.
From minima03 above... I did what mini described, and I also used usr/bin/python3 for everything and it is working FYI. I am using Xcode 12.4 on Big Sur. I use pyenv and have 3 pythons installed, system, 2.7.18 and 3.9.1.

I do get this error at the top of the output, but then it runs the python code fine after that anyway:

2021-02-12 12:28:15.725010-0500 python3[42530:1404893] Failed to open macho file at /Applications/Xcode.app/Contents/Developer/usr/bin/python3 for reading: Too many levels of symbolic links
@DrNebin

I'm having the same issue, could you please help me?

Code Block
/Applications/Xcode.app/Contents/Developer/usr/bin/python3: can't open file 'hello.py': [Errno 1] Operation not permitted
Program ended with exit code: 2

FYI - I'm a beginner

Hi. Have you added your path (/Library/Frameworks/Python.framework/Versions/3.8/bin/python3) to the variables at Search Paths -> System Header Search Paths in Building settings?

I did basically the same steps as you did SpaceGowboy, but in my case I am using python3.9, the only difference I idid (I guess, because I have been trying several different ways in the last week) was to use the path /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 all over the place (including the search path siyanhu mentioned, which in my case is called ALWAYS_SEARCH_USER_PATHS.

I also had to convert my Python3.9 symbolic link to an executable by creating a hard link (by using % ln python3.9 pythin39 as you did ( for those who do not know how to do that, you can find it at wikipedia https://en.wikipedia.org/wiki/Ln_(Unix)).

And it worked out just fine!!

  • I am running Xcode 13.2.1 and don't even know what template to select for Python. I have tried several and none seem to work.

Add a Comment