Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's not enough information here to give you the complete answer, but the basic problem is almost certainly that <code>helloClass.py</code> does not end up anywhere on the module search path, so <code>PyImport_Import</code> can't find it.</p> <p>For example, maybe you're building your <code>main</code> executable into a location like <code>~/Library/Developer/Xcode/DerivedData/Hello/Build/Products/Release/hello</code>, but not copying <code>helloClass.py</code> there. Or, maybe you <em>are</em> copying it there, but the equivalent of <code>dirname $0</code> isn't on your module path. Or… well, there are a million possibilities, but the answer is the same in every case:</p> <ol> <li><p>Figure out where the executable and the Python file are getting installed to.</p></li> <li><p>Build an appropriate (probably relative) path out of that knowledge.</p></li> <li><p>Make sure that path gets added to the module path.</p></li> </ol> <p>The exact details on how Python searches for modules is different between different versions, but the basic idea is explained in <a href="http://docs.python.org/2/reference/simple_stmts.html#import" rel="nofollow">The <code>import</code>statement</a> (or <a href="http://docs.python.org/3/reference/simple_stmts.html#import" rel="nofollow">the 3.3 version</a>, which defers to <a href="http://docs.python.org/3/reference/import.html#importsystem" rel="nofollow">The import system</a>), and often, it's just a matter of adding something to <a href="http://docs.python.org/2/library/sys.html#sys.path" rel="nofollow"><code>sys.path</code></a>.</p> <p>(Note that if you're building .app bundles instead of just Unix command-line executables, there is a standard answer to this—you stick the .py files in a standard place, use Cocoa or CoreFoundation APIs to get the path, and then use that. But that doesn't seem to be relevant here.)</p> <hr> <p>Looking at the project you uploaded at <a href="https://github.com/Eduardof0nt/help.git" rel="nofollow">https://github.com/Eduardof0nt/help.git</a>:</p> <p>You're not copying <code>helloClass.py</code> anywhere. You have to decide <em>where</em> you want to copy it, and that depends entirely on how you want to deploy things. But for debugging purposes, just to get something testable, you can just put it alongside the <code>Hello Python!</code> executable (in <code>build/Debug</code> or <code>build/Release</code>). To do that:</p> <ul> <li>Select the project in the Project Navigator.</li> <li>Select the target in the left sidebar of the project view that appears.</li> <li>Select the Build Phases tab.</li> <li>Click the Add Build Phase button.</li> <li>Add a Copy Files phase.</li> <li>Set its Destination to Products Directory.</li> <li>Drag <code>helloClass.py</code> from the Project Navigator into the build phase.</li> </ul> <p>Now, when you build, if you look at the output (turn on "All" and "All Messages"), you'll see "Copy build/Debug/helloClass.py" right after the "Link build/Debug/Hello Python!". And, if you look inside that directory, you'll see the two files side by side.</p> <p>But that doesn't solve your whole problem. Because <code>/wherever/you/started/build/Debug</code> is not going to be on the <code>sys.path</code> that Python uses. The quickest way around this is to call <code>Py_SetProgramName(argv[0])</code> right before the <code>Py_Initialize()</code>, and <code>PySys_SetArgvEx(argc, argv, 1)</code> right after. That may well not be the right thing to do for your use case, but you're going to have the read the documentation and understand what all of this does, because I can't possibly explain all the details of embedding Python in an SO answer.</p> <p>While you're trying to learn this stuff, you probably want to do some debugging. For example, you can <code>printf(path=%s\n", Py_GetPath())</code> to see what the <code>sys.path</code> equivalent is, and <code>PyObject_Print(mod, stdout, 0)</code> to see what's in <code>mod</code>, and so on. But really, you're going to want to use the Xcode debugger—add a breakpoint and try doing this stuff at runtime.</p> <p>Much of this is explained in <a href="http://docs.python.org/2/extending/index.html#extending-index" rel="nofollow">Extending and Emnbedding the Python Interpreter</a>, and you really do need to read that; there's no substitute. (You don't need to read the whole <a href="http://docs.python.org/2/c-api/index.html" rel="nofollow">Python/C API Reference Manual</a>, but you will end up reading a lot of it, and getting good at searching it, before you're done.) However, because embedding Python is much less common than extending it, the docs really don't include everything you need, so… if whatever tutorial you're using doesn't cover things like <code>PySys_*</code> and <code>Py_GetPath</code>, you may need to find a better tutorial first.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload