Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are willing to modify your question a tiny bit, you can get away with this.</p> <p>IF the only entrypoints to your package are controlled; e.g. you only test your code by doing something like invoking <code>testsuite package/.../module.py</code> which will </p> <p>THEN you can make sure that the first thing you do is <code>import firstthing</code>, and in package/firstthing.py you have:</p> <pre><code>import sys import os.path packageDir = os.path.split(__name__)[0] sys.path[:] = sys.path+[packageDir] # or maybe you want it first... </code></pre> <p>The main caveat being that you will not be able to run python files without going through your entrypoints. I always want to do this for every project I write in python (to make relative imports work nicely), but I personally find this so inconvenient that I just give up.</p> <hr> <p>There is also a second alternative. It is not <em>that</em> unreasonable to specify that your package requires another package in the python path. This package could be a utility package which performs a major hack. For example if the name of the package was "x", you could do <code>import x</code> which would use the inspect module to perform reflection on the interpreter stack, letting you figure out which module you were importing it from. Then you could do a sort of "backwards os.walk" by going up parent directories until you found the root of your package (by checking for some special indicator file, or manifest, or something). Then the code would programatically perform the above modification of the python path via <code>sys.path</code>. It's the same as the above, but you have the liberty to do things like run any python file without having to go through an awful entrypoint.</p> <p>If you have extreme control over the shell environment, you can also just augment the $PYTHONPATH to include your package directory, but this is extremely fragile in many ways, and rather inelegant.</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.
    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