0
Python relative imports  <ul>(Just let me use the file system please) <li>Stuart Mitchell </li></ul>
Whats the problem <ul><li>I have decided to make my source code be sorted in pretty directories
prog   main.py   scripts script1.py   functionalpart   __init__.py   module1.py
main can import functionalpart.module1 </li></ul><ul><li>But script1 cannot import  module1 </li></ul>
Whats the problem <ul><li>Why would I want to do this </li><ul><li>Tests
Little scripts that use functionality
Come on c lets me do it, do I have to explain. </li></ul><li>Why not </li><ul><li>Looks ugly
Could lead to namespace clashes </li><ul><li>Import foo
Upcoming SlideShare
Loading in...5
×

Python relative imports (Just let me use the file system please)

10,414

Published on

A quick description how one can alter the import path to make packages/modules accessible on the file system. Includes these:

*

PYHTHONPATH environment variable
*

sys.path variable in the Python runtime
*

file system hierarchy and module accessibility inside (site-/dist-packages, local packages, magic of the __init__.py file, ...)
*

using .pth files
*

using setup.py and doing the Packaging Mambo (.deb files, eggs/PyPI packages, ...)

In conclusion after some discussion in the meeting I have decided that the python import statement is not about the file system. This is why it is impossible to give it a filename to import. The import statement is about importing a namespace into your code. The question then becomes how does python find your module to import the namespace. In this case setup.py is the proper way to do things. Use

$python setup.py develop

to make the your modules visible to python and use buildout or virtualenv to sandbox your developement environment from your actual python installation.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
10,414
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Python relative imports (Just let me use the file system please)"

  1. 1. Python relative imports <ul>(Just let me use the file system please) <li>Stuart Mitchell </li></ul>
  2. 2. Whats the problem <ul><li>I have decided to make my source code be sorted in pretty directories
  3. 3. prog main.py scripts script1.py functionalpart __init__.py module1.py
  4. 4. main can import functionalpart.module1 </li></ul><ul><li>But script1 cannot import module1 </li></ul>
  5. 5. Whats the problem <ul><li>Why would I want to do this </li><ul><li>Tests
  6. 6. Little scripts that use functionality
  7. 7. Come on c lets me do it, do I have to explain. </li></ul><li>Why not </li><ul><li>Looks ugly
  8. 8. Could lead to namespace clashes </li><ul><li>Import foo
  9. 9. Import ../foo </li></ul><li>Forces you to be sensible about your module names </li></ul></ul>
  10. 10. Possible Solutions <ul><li>Symlinks </li><ul><li>Place symlinks to functionalpart directory in scripts
  11. 11. Place symlinks to trunk in /usr/local/lib/python2.6/dist-packages/
  12. 12. Alows script1 to import prog.functionalpart or import functional part depending on links </li></ul></ul><ul><ul><li>Doesn't work in windows
  13. 13. 2 nd solution will mask the working version of your module with the developement one </li></ul></ul><ul><li>.pth files </li></ul><ul><ul><li>Don't work from a local directory </li></ul></ul>
  14. 14. Possible Solutions <ul><li>PYTHONPATH </li><ul><li>Append / prepend paths to the PYTHONPATH Variable
  15. 15. Alows script1 to import prog.functionalpart </li></ul></ul><ul><ul><li>This is a hassle if you do it each time
  16. 16. If this is permanent can mask the working version
  17. 17. Hassle if you move your location </li></ul></ul>
  18. 18. Possible Solutions <ul><li>sys.path manipulations </li><ul><li>Put this at the top of the file
  19. 19. import sys import os sys.path.insert(0, &quot;..&quot;) sys.path.insert(0, os.path.join([&quot;..&quot;,&quot;..&quot;]))
  20. 20. Or better yet put this in a module and import it.
  21. 21. Allows import functionalpath </li></ul></ul><ul><ul><li>Ugly piece of code
  22. 22. Remember that sys.path is global </li></ul></ul>
  23. 23. Possible Solutions <ul><li>Python relative imports </li><ul><li>PEP: 328 (Does not seem to follow the syntax though)
  24. 24. Python 2.4 </li></ul><li> runk main.py ests __init__.py test1.py functionalpart __init__.py module1.py </li><ul><li>In test1 you can from .functionalpart import module1
  25. 25. Also from .main import dostuff </li></ul></ul><ul><ul><li>Only if tests is imported as a package </li></ul></ul><ul><ul><li>Does not work if test1.py should be run as a script
  26. 26. Does not allow from . import main </li></ul></ul>
  27. 27. Possible Solutions <ul><li>setup.py </li><ul><li>Or some other build tool
  28. 28. Used together with buildout or virtualenv you can ensure that you can import your own local packages
  29. 29. Uses import prog.functionalpart.module1
  30. 30. Probably is the recommended route
  31. 31. Forces you to make and use a proper setup.py (this is a good thing)
  32. 32. Pay the setup.py tax then forget about it </li></ul></ul><ul><ul><li>Hard work (this is a bad thing) </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×