{ "info": { "author": "Joaquin Duo", "author_email": "joaduo@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "The rel_imp utility\n====================\n\n|Codeship Status for joaduo/rel_imp|\n\nEnabling explicit relative imports in main modules the easy way.\n\nFor enabling relative import in ``__main__`` module PEP 366 presents a\nworkaround like:\n\n.. code:: python\n\n if __name__ == \"__main__\" and __package__ is None:\n __package__ = \"my_pkg\"\n\n from .foo import bar\n\nWith ``rel_imp`` you can rewrite it as:\n\n.. code:: python\n\n import rel_imp; rel_imp.init()\n from .foo import bar\n\nCleaner, faster and less coupled. (you don't need to specify the package\nmanually)\n\n**Note:** In order to use ``rel_imp`` the module you are coding must be\ninside a package or a sub-package.\n\nWhy using explicit relative imports?\n------------------------------------\n\nPython 2 still supports implicit relative import and will be deprecated\nin Python 3, so you will want to migrate those scripts using implicit\nrelative import to explicit relative import. Check more on the `PEP\n404 `__.\n\nExplicit relative imports makes your code less coupled. As `PEP\n328 `__\nsays:\n\n Several use cases were presented, the most important of which is\n being able to rearrange the structure of large packages without\n having to edit sub-packages. In addition, a module inside a package\n can't easily import itself without relative imports.\n\nAlthough it is still a matter of taste. (I personally prefer less code\nto express the same)\n\nWhy running sub-modules as main?\n--------------------------------\n\nSome reasons:\n\n1. A submodule can become a command line tool if called as main.\n2. You can have unit test or smoke test within the module.\n3. You simply want to run it without any explicit test to see if at\n least it imports everything it needs.\n\nInstallation and Uninstallation\n-------------------------------\n\nInstall via pip\n~~~~~~~~~~~~~~~\n\n::\n\n pip install rel_imp\n\nRemove it with:\n\n::\n\n pip uninstall rel_imp\n\nInstall downloading the file\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nDownload the ``rel_imp.py`` in one your Python's search path.\n\n::\n\n wget https://raw.githubusercontent.com/joaduo/rel_imp/master/rel_imp.py\n\nExample\n-------\n\nImagine you have 2 modules inside a package called \"my_pkg\".\n\nThat would be:\n\n- ``my_pkg/__init__.py``\n- ``my_pkg/math_lib.py``\n- ``my_pkg/test.py``\n\nSo in test.py we could have\n\n.. code:: python\n\n from .math_lib import factorize\n\n def factorize_and_print(number):\n num = factorize(number)\n print num\n\n if __name__ == '__main__':\n #Small smoke test\n factorize_and_print(10)\n factorize_and_print(0)\n factorize_and_print(-10)\n\nIf you do ``python my_pkg/test.py`` it will throw an exception because\nof the relative import at the first line.\n\nSo you can use ``rel_imp`` to make your code look nicer. Simply do:\n\n.. code:: python\n\n import rel_imp; rel_imp.init()\n from .math_lib import factorize\n\nIt is equivalent as the prior solution but you don't have to worry about\nkeeping in sync ``__package__``'s value.\n\nHow does it work?\n-----------------\n\nIt uses the same technique in PEP 366 but ``__package__``'s value is set\nthrough dynamic inspection of the stack. To solve the value of\n``__package__`` it compares the current ``__main__``'s file with search\npaths in sys.path - or, optionally, a list of paths given in the\nsettings -.\n\nFor example, for a file in\n``/home/user/projects/python/math/my_pkg/test.py`` given the following\npaths in sys.path:\n\n.. code:: python\n\n [\n '/home/user/projects/python/',\n '/home/user/projects/python/math/',\n '/home/user/projects/python/math/my_pkg/',\n ]\n\nIt will pick the closest path to the ``__main__``'s file that is not the\n``__main__``'s file's directory.\n\nThen the base path use to solve ``__package__`` variable will be\n``/home/user/projects/python/math/``\n\nDisabling rel_imp\n------------------\n\nRelative import shouldn't have any non-desired side effect, but if for\nsome reason you want to disable it:\n\n1. Replace the ``def init()`` function in the ``rel_imp.py`` file (if\n you downloaded it)\n2. Create an empty ``rel_imp.py`` with a dummy ``init()`` in a\n PYTHONPATH with higher priority than the installed one (or optionally\n uninstall the original one)\n\n``rel_imp`` is really lightweight so you won't gain any performance\ndifference disabling it.\n\nFeedback or bugs reporting\n--------------------------\n\nFile an issue through github's `issue tracking\nsystem `__.\n\nYou can optionally contact me at joaduo gmail com.\n\nRelated PEPs\n------------\n\n- http://legacy.python.org/dev/peps/pep-0328/\n- http://legacy.python.org/dev/peps/pep-0366/\n\n.. |Codeship Status for joaduo/rel_imp| image:: https://codeship.com/projects/6634b790-b2f6-0132-44e8-3a4b0bbda320/status?branch=master\n :target: https://codeship.com/projects/70108", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/joaduo/rel_imp", "keywords": "explicit,relative,import", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "rel_imp", "package_url": "https://pypi.org/project/rel_imp/", "platform": "", "project_url": "https://pypi.org/project/rel_imp/", "project_urls": { "Homepage": "https://github.com/joaduo/rel_imp" }, "release_url": "https://pypi.org/project/rel_imp/0.2.7/", "requires_dist": null, "requires_python": "", "summary": "Enable explicit relative imports in __main__ module.", "version": "0.2.7" }, "last_serial": 3983065, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "e3e2474e953167e8a0a6c20458d045ac", "sha256": "011480b893170e6f561b05d10173f7450aca4e832d3ab6c606a8d37df393cefe" }, "downloads": -1, "filename": "rel_imp-0.1.tar.gz", "has_sig": false, "md5_digest": "e3e2474e953167e8a0a6c20458d045ac", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5787, "upload_time": "2014-09-25T23:36:25", "url": "https://files.pythonhosted.org/packages/cd/d8/7a79b24d44d167f5cb9ae72270a48309a09a51e441fe9952dfbdd0ea1bb3/rel_imp-0.1.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "82a1c305e28883e6b483b56b98d59527", "sha256": "1f152eea5d9352e2673541855fb6cb97fdc07bd59d7f6d2450851a22eec769b9" }, "downloads": -1, "filename": "rel_imp-0.1.1.tar.gz", "has_sig": false, "md5_digest": "82a1c305e28883e6b483b56b98d59527", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6035, "upload_time": "2014-09-26T05:02:38", "url": "https://files.pythonhosted.org/packages/98/e5/936785d2a8404d1f26cd4f1dbd638b00afe5a6c2f6ab037f52a427f31b54/rel_imp-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "97d485f6d6e6a05922bddfe922e6cc48", "sha256": "277182986696c4975bd512d6426bf8eae8134405980b79a3ad1e7233a6c7a250" }, "downloads": -1, "filename": "rel_imp-0.1.2.tar.gz", "has_sig": false, "md5_digest": "97d485f6d6e6a05922bddfe922e6cc48", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6013, "upload_time": "2014-09-29T19:59:48", "url": "https://files.pythonhosted.org/packages/12/a5/2f520fd9943da89065710d4e487f1aa048022feda0fa43531ac292e3ceb8/rel_imp-0.1.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "73da2736137b76d15b3911b6e8f90705", "sha256": "22accca720bdd2461144b9a654c93f3079a14644198807994e1cd8b25cbdb499" }, "downloads": -1, "filename": "rel_imp-0.2.1.tar.gz", "has_sig": false, "md5_digest": "73da2736137b76d15b3911b6e8f90705", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6847, "upload_time": "2016-06-09T16:20:11", "url": "https://files.pythonhosted.org/packages/bf/86/4e1a3039943dff02e01850fb323de481c6a1e304d48da9af67953f1bce05/rel_imp-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "e8b98f836f545955bbec6d799d4a00aa", "sha256": "0755441ceca12b54fe661a8a2ced696908638a52f2e50f7efcc62262fba44aab" }, "downloads": -1, "filename": "rel_imp-0.2.2.tar.gz", "has_sig": false, "md5_digest": "e8b98f836f545955bbec6d799d4a00aa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6966, "upload_time": "2016-06-13T13:41:22", "url": "https://files.pythonhosted.org/packages/84/2e/8228f37739e3d2b17d61fd053bca4d17709b99a804ac434443d9aa342f6a/rel_imp-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "5e71f36dbec772e25df8da83d33b4b7b", "sha256": "d7f5599d133b04bb8f3ef0aad29666599feb9d228f9f992aa3df82bdc0a3df1f" }, "downloads": -1, "filename": "rel_imp-0.2.3.tar.gz", "has_sig": false, "md5_digest": "5e71f36dbec772e25df8da83d33b4b7b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6994, "upload_time": "2016-06-13T15:05:19", "url": "https://files.pythonhosted.org/packages/2a/6a/edecef9a55abd0627fa5f099bbe3cc0924e93d3727041c6e4df50bffd40d/rel_imp-0.2.3.tar.gz" } ], "0.2.4": [ { "comment_text": "", "digests": { "md5": "a5d3fd0895e237dbdbc1c8dc5ba82d80", "sha256": "177a6d94cf2a49056bfb27f4ded84e4004f147aa415fb266feea1ff55d05cf69" }, "downloads": -1, "filename": "rel_imp-0.2.4.tar.gz", "has_sig": false, "md5_digest": "a5d3fd0895e237dbdbc1c8dc5ba82d80", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6998, "upload_time": "2016-06-18T02:41:23", "url": "https://files.pythonhosted.org/packages/11/e9/ab5502a8d01fd0c76a7c18e4b27eaa4dd9e9afd4d7dee858e36d1bcbd83e/rel_imp-0.2.4.tar.gz" } ], "0.2.6": [ { "comment_text": "", "digests": { "md5": "4d2d4496a39cbe37c3e48339c31d7abf", "sha256": "6fe6463dabc84ffe390226569f13aff7b0a9c1744d4afa6a30be1296615bdc7b" }, "downloads": -1, "filename": "rel_imp-0.2.6.tar.gz", "has_sig": false, "md5_digest": "4d2d4496a39cbe37c3e48339c31d7abf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6645, "upload_time": "2017-04-07T08:15:45", "url": "https://files.pythonhosted.org/packages/ec/25/a2889c7d1822d96548b3809559dc94d614c9fc5e5332c19b892a0cb4efce/rel_imp-0.2.6.tar.gz" } ], "0.2.7": [ { "comment_text": "", "digests": { "md5": "f975213e1237ee1187670086bdeebf8e", "sha256": "08be01476c014cd9bd8af3174dd5b0f26c8cc8d3dda5bf15fc0e65f335f58ac9" }, "downloads": -1, "filename": "rel_imp-0.2.7.tar.gz", "has_sig": false, "md5_digest": "f975213e1237ee1187670086bdeebf8e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6657, "upload_time": "2018-06-20T21:39:57", "url": "https://files.pythonhosted.org/packages/65/9b/db7aba3158667c9646cda8211f30282d41bcaff7babe2f5c10e2dfc82f87/rel_imp-0.2.7.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f975213e1237ee1187670086bdeebf8e", "sha256": "08be01476c014cd9bd8af3174dd5b0f26c8cc8d3dda5bf15fc0e65f335f58ac9" }, "downloads": -1, "filename": "rel_imp-0.2.7.tar.gz", "has_sig": false, "md5_digest": "f975213e1237ee1187670086bdeebf8e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6657, "upload_time": "2018-06-20T21:39:57", "url": "https://files.pythonhosted.org/packages/65/9b/db7aba3158667c9646cda8211f30282d41bcaff7babe2f5c10e2dfc82f87/rel_imp-0.2.7.tar.gz" } ] }