beeware / Python-Apple-support

A meta-package for building a version of Python that can be embedded into a macOS, iOS, tvOS or watchOS project.
MIT License
1.12k stars 164 forks source link

'cpython/initconfig.h' file not found #96

Closed wushenchao closed 3 years ago

wushenchao commented 4 years ago

Describe the bug When I used it in the project, the following error occurred, maybe the way I introduced is wrong, who can help me, thank you!

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

截屏2020-08-0216 58 27

Environment:

Additional context Add any other context about the problem here.

Gerzer commented 3 years ago

I'm having this issue, too. Did you ever find a solution?

freakboy3742 commented 3 years ago

What is "this issue"?

It's almost impossible to provide any assistance when we're provided no detail on what you've done, and what you expected. Despite a bug form that explicitly asks for detailed reproduction instructions, software versions, and additional context, all I have to go on is a screenshot of an IDE that suggests that a header file wasn't found.

To make my point, allow me to reply to this report with the same attention of detail that the original report contained:

The problem is that you need to be able to find the header files. Make the project find the header files, and the problem will go away.

That wasn't very helpful, was it? If you want more assistance, you need to tell us how you got this error in the first place.

Gerzer commented 3 years ago

What is "this issue"?

It's almost impossible to provide any assistance when we're provided no detail on what you've done, and what you expected. Despite a bug form that explicitly asks for detailed reproduction instructions, software versions, and additional context, all I have to go on is a screenshot of an IDE that suggests that a header file wasn't found.

To make my point, allow me to reply to this report with the same attention of detail that the original report contained:

The problem is that you need to be able to find the header files. Make the project find the header files, and the problem will go away.

That wasn't very helpful, was it? If you want more assistance, you need to tell us how you got this error in the first place.

I don't know the exact context for the problem that @wushenchao has, but I can provide some clarification for my own situation, which perfectly matches all of the information that @wushenchao did provide.

I have a standard SwiftUI Xcode project that has been barely modified from the template. I added ".framework" to the ends of the folder names (e.g., I renamed the "Python" folder to "Python.framework") in the Python 3.8 package from this repository. I then copied all four framework folders into my Xcode project directory. I then added the respective ".a" files to the linking phase in the "Build Phases" tab in Xcode. Finally, I added #import <Python/Python.h> at the top of one of my Objective-C header files. Trying to build the project results in the error that's shown in the screenshot that @wushenchao provided.

I also attempted a few troubleshooting steps. I tried linking against the ".framework" folders instead of the ".a" files, but that just resulted in regular "Python.h not found" errors. I tried moving the import statement to an implementation file instead of a header file, but I got the same "initconfig" error. I also tried replacing "import" with "include", but that didn't work, either.

You didn't have to be rude in your response. The original post indeed wasn't very detailed, but you could've been more courteous anyway.

freakboy3742 commented 3 years ago

I apologise if my response was short. But believe me, it is immensely frustrating when the community at large seems to believe that FLOSS maintainers have some sort of magical mind reading powers.

Anyway; moving on.

Renaming "Python" to "Python.framework" won't ever work, because the Python support library isn't packaged as a framework. Frameworks have additional layout and metadata requirements that the Python support libraries don't provide. This is for historical reasons; prior to iOS 8, you weren't allowed to use Frameworks at all. The .a files need to be treated as static libraries, and the include paths for your project need to be updated to point at the appropriate paths.

I can't comment on the interaction with SwiftUI, though. The best point of reference I can give you is to use a project generated from a Briefcase template as a starting point.

Gerzer commented 3 years ago

I apologise if my response was short. But believe me, it is immensely frustrating when the community at large seems to believe that FLOSS maintainers have some sort of magical mind reading powers.

Anyway; moving on.

Renaming "Python" to "Python.framework" won't ever work, because the Python support library isn't packaged as a framework. Frameworks have additional layout and metadata requirements that the Python support libraries don't provide. This is for historical reasons; prior to iOS 8, you weren't allowed to use Frameworks at all. The .a files need to be treated as static libraries, and the include paths for your project need to be updated to point at the appropriate paths.

I can't comment on the interaction with SwiftUI, though. The best point of reference I can give you is to use a project generated from a Briefcase template as a starting point.

Thanks for replying so quickly. The SwiftUI component isn't actually relevant here; the issue is with importing the Python framework in general, not with its interaction with any other framework.

As I mentioned in my comment, I did try linking against the ".a" static libraries, which is what produced the error in question. I could be wrong, but it seems to me that while I can successfully import the "Python.h" header in a general sense, other headers that "Python.h" in turn imports (whether directly or indirectly) can't be found. To reproduce the issue, I would recommend that you create a new Xcode project from one of the basic iOS templates (it shouldn't matter which specific one you choose), link against the ".a" static libraries, and try to import the "Python.h" header. If it fails with the same error message, then you've successfully reproduced this issue and perhaps could investigate it. If it succeeds, then it's possible that I did something wrongly, in which case I would appreciate it if you could post your working Xcode project so that I can compare it to my own.

I genuinely appreciate your help. If there's any other information that you might need, then just say so. I could post my faulty Xcode project if you want.

freakboy3742 commented 3 years ago

Like I said - you can generate a working stub empty project that works using Briefcase. If you don't want to go down the full Briefcase path, you can manually roll out the template by following the instructions in the template README. That template is nothing more than a fresh iOS Xcode project that has been modified to add the Python support library, with all the project settings configured as required, and main.m file modified to start a Python application.

wushenchao commented 3 years ago

I'm having this issue, too. Did you ever find a solution?

First of all I want to thank you, I did not solve this problem, I ended up using version 3.7。

wushenchao commented 3 years ago

Like I said - you can generate a working stub empty project that works using Briefcase. If you don't want to go down the full Briefcase path, you can manually roll out the template by following the instructions in the template README. That template is nothing more than a fresh iOS Xcode project that has been modified to add the Python support library, with all the project settings configured as required, and main.m file modified to start a Python application.

I created the project using Python-iOS-template. The error only appeared when I introduced 3.8. In the same way, when I lowered the version to 3.7, the error disappeared. If needed, I can provide my test project.

freakboy3742 commented 3 years ago

@wushenchao If the screenshot you've provided is of the project when it was failing, the problem is that you didn't unpack the support package into the correct location. You can see the "Support" item in the project tree is a missing link; the Python/BZip etc folders should be contained under the Support folder.

Gerzer commented 3 years ago

@wushenchao If the screenshot you've provided is of the project when it was failing, the problem is that you didn't unpack the support package into the correct location. You can see the "Support" item in the project tree is a missing link; the Python/BZip etc folders should be contained under the Support folder.

Is there an easy way to customize the search paths so that I can, say, name the "Support" directory something else? In general, how flexible is the directory structure?

freakboy3742 commented 3 years ago

@Gerzer Completely flexible. There's nothing hard-coded in the support package that expects that location; the "Support" folder name is a convention that Briefcase uses when packaging apps, and the Xcode project it generates uses that convention, but if you want to use an entirely different directory structure, you can.

rickmark commented 2 years ago

Re-opening this issue as it occurs with Python 3.9.7 from Homebrew...

So I think I understand what is going on here. Homebrew creates a Python.framework in the correct layout (versions, headers, dylibs) but recent versions of the clang compiler hate two things:

Using "double quote" includes for framework includes (it's opinionated here because double quotes are for the "library consumer"'s includes not for "system includes" and python, when being used as a library and not an executable is akin to a library so it should have library header import conventions...) This one is "fixable" though the "Quoted Include in Framework Header" build setting.

The second problem is the way that frameworks do their include paths. While it is normal to be able to include a relative subdirectory file like cpython/initconfig.h if you are using explicit C include directories, clang / apple want to see this imported ad #include <Python/cpython/initconfig.h> with the name of the framework preceding it. I'm pretty sure that clang is in the wrong here, but this should help people understand what is happening and why.

One potential fix would, as was suggested in the bug tracker, to flatten the tree (move all of cypthon headers into the include path, and prefix with cpython_). One could even leave files at the existing locations to provide source compatibility

freakboy3742 commented 2 years ago

I'm not sure I understand how "from homebrew" is relevant here. Python-Apple-support is a complete standalone build of Python. It shouldn't be using anything from homebrew.

Radical changes to the source layout of CPython isn't a good option either. Some accomodations are obvious needed to support iOS, tvOS and watchOS builds, but those should be in the form of small patches adding extra branches in the code, not wholesale restructuring.

Lastly - nothing in this project should be creating a Python.Framework. I have no idea how you'd even get a framework build out of the artefacts of this project.

So... I'm very confused about what is being reported here.

rickmark commented 2 years ago

Sorry let me clarify several things....

"From Homebrew" - this means from the formula that compiles CPython and places the binary / dylib / headers at /usr/local/opt/python on macOS. This is 100% not the Python.framework that comes with macOS but built from FOSS python from Git

"Building the Python.framework" comes from cpython having the make target of frameworkinstall and frameworkinstallextra - this means yes part of a "passing build" of CPython on macOS is in fact building the framework package layout, currently it does not build one that can be used, therefore it's a defect (https://github.com/python/cpython/blob/470145f572b53fe73518cda1eeacc56fec78c1b2/Mac/README.rst)

freakboy3742 commented 2 years ago

Sure... but that's not what this project does.

This project builds a very specific support package that is used by Briefcase, and could be used in other contexts. There is no option to turn on framework builds. The output of this project is not a Framework build.

It sounds very much like you're reporting a bug against a different project, because the terms you're using and the way you're describing the problem doesn't seem to map at all to what this project actually does...

rickmark commented 2 years ago

Yep - I screwed up - I read the issue, and it is 100% an issue with cypthon itself, but just went straight at trying to solve it - crud