Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DESCRIPTION.rst 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. .. image:: https://raw.github.com/timothycrosley/isort/master/logo.png
  2. :alt: isort
  3. ########
  4. .. image:: https://badge.fury.io/py/isort.svg
  5. :target: https://badge.fury.io/py/isort
  6. :alt: PyPI version
  7. .. image:: https://travis-ci.org/timothycrosley/isort.svg?branch=master
  8. :target: https://travis-ci.org/timothycrosley/isort
  9. :alt: Build Status
  10. .. image:: https://coveralls.io/repos/timothycrosley/isort/badge.svg?branch=release%2F2.6.0&service=github
  11. :target: https://coveralls.io/github/timothycrosley/isort?branch=release%2F2.6.0
  12. :alt: Coverage
  13. .. image:: https://img.shields.io/github/license/mashape/apistatus.svg
  14. :target: https://pypi.python.org/pypi/hug/
  15. :alt: License
  16. .. image:: https://badges.gitter.im/Join%20Chat.svg
  17. :alt: Join the chat at https://gitter.im/timothycrosley/isort
  18. :target: https://gitter.im/timothycrosley/isort?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
  19. isort your python imports for you so you don't have to.
  20. isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections.
  21. It provides a command line utility, Python library and `plugins for various editors <https://github.com/timothycrosley/isort/wiki/isort-Plugins>`_ to quickly sort all your imports.
  22. It currently cleanly supports Python 2.7 - 3.6 without any dependencies.
  23. .. image:: https://raw.github.com/timothycrosley/isort/develop/example.gif
  24. :alt: Example Usage
  25. Before isort:
  26. .. code-block:: python
  27. from my_lib import Object
  28. print("Hey")
  29. import os
  30. from my_lib import Object3
  31. from my_lib import Object2
  32. import sys
  33. from third_party import lib15, lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14
  34. import sys
  35. from __future__ import absolute_import
  36. from third_party import lib3
  37. print("yo")
  38. After isort:
  39. .. code-block:: python
  40. from __future__ import absolute_import
  41. import os
  42. import sys
  43. from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8,
  44. lib9, lib10, lib11, lib12, lib13, lib14, lib15)
  45. from my_lib import Object, Object2, Object3
  46. print("Hey")
  47. print("yo")
  48. Installing isort
  49. ================
  50. Installing isort is as simple as:
  51. .. code-block:: bash
  52. pip install isort
  53. or if you prefer
  54. .. code-block:: bash
  55. easy_install isort
  56. Using isort
  57. ===========
  58. **From the command line**:
  59. .. code-block:: bash
  60. isort mypythonfile.py mypythonfile2.py
  61. or recursively:
  62. .. code-block:: bash
  63. isort -rc .
  64. *which is equivalent to:*
  65. .. code-block:: bash
  66. isort **/*.py
  67. or to see the proposed changes without applying them:
  68. .. code-block:: bash
  69. isort mypythonfile.py --diff
  70. Finally, to atomically run isort against a project, only applying changes if they don't introduce syntax errors do:
  71. .. code-block:: bash
  72. isort -rc --atomic .
  73. (Note: this is disabled by default as it keeps isort from being able to run against code written using a different version of Python)
  74. **From within Python**:
  75. .. code-block:: bash
  76. from isort import SortImports
  77. SortImports("pythonfile.py")
  78. or:
  79. .. code-block:: bash
  80. from isort import SortImports
  81. new_contents = SortImports(file_contents=old_contents).output
  82. **From within Kate:**
  83. .. code-block:: bash
  84. ctrl+[
  85. or:
  86. .. code-block:: bash
  87. menu > Python > Sort Imports
  88. Installing isort's Kate plugin
  89. ==============================
  90. For KDE 4.13+ / Pate 2.0+:
  91. .. code-block:: bash
  92. wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin.py --output-document ~/.kde/share/apps/kate/pate/isort_plugin.py
  93. wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin_ui.rc --output-document ~/.kde/share/apps/kate/pate/isort_plugin_ui.rc
  94. wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/katepart_isort.desktop --output-document ~/.kde/share/kde4/services/katepart_isort.desktop
  95. For all older versions:
  96. .. code-block:: bash
  97. wget https://raw.github.com/timothycrosley/isort/master/kate_plugin/isort_plugin_old.py --output-document ~/.kde/share/apps/kate/pate/isort_plugin.py
  98. You will then need to restart kate and enable Python Plugins as well as the isort plugin itself.
  99. Installing isort's for your preferred text editor
  100. =================================================
  101. Several plugins have been written that enable to use isort from within a variety of text-editors.
  102. You can find a full list of them `on the isort wiki <https://github.com/timothycrosley/isort/wiki/isort-Plugins>`_.
  103. Additionally, I will enthusiastically accept pull requests that include plugins for other text editors
  104. and add documentation for them as I am notified.
  105. How does isort work?
  106. ====================
  107. isort parses specified files for global level import lines (imports outside of try / except blocks, functions, etc..)
  108. and puts them all at the top of the file grouped together by the type of import:
  109. - Future
  110. - Python Standard Library
  111. - Third Party
  112. - Current Python Project
  113. - Explicitly Local (. before import, as in: ``from . import x``)
  114. - Custom Separate Sections (Defined by forced_separate list in configuration file)
  115. - Custom Sections (Defined by sections list in configuration file)
  116. Inside of each section the imports are sorted alphabetically. isort automatically removes duplicate python imports,
  117. and wraps long from imports to the specified line length (defaults to 80).
  118. When will isort not work?
  119. =========================
  120. If you ever have the situation where you need to have a try / except block in the middle of top-level imports or if
  121. your import order is directly linked to precedence.
  122. For example: a common practice in Django settings files is importing * from various settings files to form
  123. a new settings file. In this case if any of the imports change order you are changing the settings definition itself.
  124. However, you can configure isort to skip over just these files - or even to force certain imports to the top.
  125. Configuring isort
  126. =================
  127. If you find the default isort settings do not work well for your project, isort provides several ways to adjust
  128. the behavior.
  129. To configure isort for a single user create a ``~/.isort.cfg`` file:
  130. .. code-block:: ini
  131. [settings]
  132. line_length=120
  133. force_to_top=file1.py,file2.py
  134. skip=file3.py,file4.py
  135. known_future_library=future,pies
  136. known_standard_library=std,std2
  137. known_third_party=randomthirdparty
  138. known_first_party=mylib1,mylib2
  139. indent=' '
  140. multi_line_output=3
  141. length_sort=1
  142. forced_separate=django.contrib,django.utils
  143. default_section=FIRSTPARTY
  144. no_lines_before=LOCALFOLDER
  145. Additionally, you can specify project level configuration simply by placing a ``.isort.cfg`` file at the root of your
  146. project. isort will look up to 25 directories up, from the file it is ran against, to find a project specific configuration.
  147. Or, if you prefer, you can add an isort section to your project's ``setup.cfg`` or ``tox.ini`` file with any desired settings.
  148. You can then override any of these settings by using command line arguments, or by passing in override values to the
  149. SortImports class.
  150. Finally, as of version 3.0 isort supports editorconfig files using the standard syntax defined here:
  151. http://editorconfig.org/
  152. Meaning you place any standard isort configuration parameters within a .editorconfig file under the ``*.py`` section
  153. and they will be honored.
  154. For a full list of isort settings and their meanings `take a look at the isort wiki <https://github.com/timothycrosley/isort/wiki/isort-Settings>`_.
  155. Multi line output modes
  156. =======================
  157. You will notice above the "multi_line_output" setting. This setting defines how from imports wrap when they extend
  158. past the line_length limit and has 6 possible settings:
  159. **0 - Grid**
  160. .. code-block:: python
  161. from third_party import (lib1, lib2, lib3,
  162. lib4, lib5, ...)
  163. **1 - Vertical**
  164. .. code-block:: python
  165. from third_party import (lib1,
  166. lib2,
  167. lib3
  168. lib4,
  169. lib5,
  170. ...)
  171. **2 - Hanging Indent**
  172. .. code-block:: python
  173. from third_party import \
  174. lib1, lib2, lib3, \
  175. lib4, lib5, lib6
  176. **3 - Vertical Hanging Indent**
  177. .. code-block:: python
  178. from third_party import (
  179. lib1,
  180. lib2,
  181. lib3,
  182. lib4,
  183. )
  184. **4 - Hanging Grid**
  185. .. code-block:: python
  186. from third_party import (
  187. lib1, lib2, lib3, lib4,
  188. lib5, ...)
  189. **5 - Hanging Grid Grouped**
  190. .. code-block:: python
  191. from third_party import (
  192. lib1, lib2, lib3, lib4,
  193. lib5, ...
  194. )
  195. **6 - NOQA**
  196. .. code-block:: python
  197. from third_party import lib1, lib2, lib3, ... # NOQA
  198. Alternatively, you can set ``force_single_line`` to ``True`` (``-sl`` on the command line) and every import will appear on its
  199. own line:
  200. .. code-block:: python
  201. from third_party import lib1
  202. from third_party import lib2
  203. from third_party import lib3
  204. ...
  205. Note: to change the how constant indents appear - simply change the indent property with the following accepted formats:
  206. * Number of spaces you would like. For example: 4 would cause standard 4 space indentation.
  207. * Tab
  208. * A verbatim string with quotes around it.
  209. For example:
  210. .. code-block:: python
  211. " "
  212. is equivalent to 4.
  213. For the import styles that use parentheses, you can control whether or not to
  214. include a trailing comma after the last import with the ``include_trailing_comma``
  215. option (defaults to ``False``).
  216. Intelligently Balanced Multi-line Imports
  217. =========================================
  218. As of isort 3.1.0 support for balanced multi-line imports has been added.
  219. With this enabled isort will dynamically change the import length to the one that produces the most balanced grid,
  220. while staying below the maximum import length defined.
  221. Example:
  222. .. code-block:: python
  223. from __future__ import (absolute_import, division,
  224. print_function, unicode_literals)
  225. Will be produced instead of:
  226. .. code-block:: python
  227. from __future__ import (absolute_import, division, print_function,
  228. unicode_literals)
  229. To enable this set ``balanced_wrapping`` to ``True`` in your config or pass the ``-e`` option into the command line utility.
  230. Custom Sections and Ordering
  231. ============================
  232. You can change the section order with ``sections`` option from the default of:
  233. .. code-block:: ini
  234. FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
  235. to your preference:
  236. .. code-block:: ini
  237. sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER
  238. You also can define your own sections and their order.
  239. Example:
  240. .. code-block:: ini
  241. known_django=django
  242. known_pandas=pandas,numpy
  243. sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,PANDAS,FIRSTPARTY,LOCALFOLDER
  244. would create two new sections with the specified known modules.
  245. The ``no_lines_before`` option will prevent the listed sections from being split from the previous section by an empty line.
  246. Example:
  247. .. code-block:: ini
  248. sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
  249. no_lines_before=LOCALFOLDER
  250. would produce a section with both FIRSTPARTY and LOCALFOLDER modules combined.
  251. Auto-comment import sections
  252. ============================
  253. Some projects prefer to have import sections uniquely titled to aid in identifying the sections quickly
  254. when visually scanning. isort can automate this as well. To do this simply set the ``import_heading_{section_name}``
  255. setting for each section you wish to have auto commented - to the desired comment.
  256. For Example:
  257. .. code-block:: ini
  258. import_heading_stdlib=Standard Library
  259. import_heading_firstparty=My Stuff
  260. Would lead to output looking like the following:
  261. .. code-block:: python
  262. # Standard Library
  263. import os
  264. import sys
  265. import django.settings
  266. # My Stuff
  267. import myproject.test
  268. Ordering by import length
  269. =========================
  270. isort also makes it easy to sort your imports by length, simply by setting the ``length_sort`` option to ``True``.
  271. This will result in the following output style:
  272. .. code-block:: python
  273. from evn.util import (
  274. Pool,
  275. Dict,
  276. Options,
  277. Constant,
  278. DecayDict,
  279. UnexpectedCodePath,
  280. )
  281. Skip processing of imports (outside of configuration)
  282. =====================================================
  283. To make isort ignore a single import simply add a comment at the end of the import line containing the text ``isort:skip``:
  284. .. code-block:: python
  285. import module # isort:skip
  286. or:
  287. .. code-block:: python
  288. from xyz import (abc, # isort:skip
  289. yo,
  290. hey)
  291. To make isort skip an entire file simply add ``isort:skip_file`` to the module's doc string:
  292. .. code-block:: python
  293. """ my_module.py
  294. Best module ever
  295. isort:skip_file
  296. """
  297. import b
  298. import a
  299. Adding an import to multiple files
  300. ==================================
  301. isort makes it easy to add an import statement across multiple files, while being assured it's correctly placed.
  302. From the command line:
  303. .. code-block:: bash
  304. isort -a "from __future__ import print_function" *.py
  305. from within Kate:
  306. .. code-block::
  307. ctrl+]
  308. or:
  309. .. code-block::
  310. menu > Python > Add Import
  311. Removing an import from multiple files
  312. ======================================
  313. isort also makes it easy to remove an import from multiple files, without having to be concerned with how it was originally
  314. formatted.
  315. From the command line:
  316. .. code-block:: bash
  317. isort -r "os.system" *.py
  318. from within Kate:
  319. .. code-block::
  320. ctrl+shift+]
  321. or:
  322. .. code-block::
  323. menu > Python > Remove Import
  324. Using isort to verify code
  325. ==========================
  326. The ``--check-only`` option
  327. ---------------------------
  328. isort can also be used to used to verify that code is correctly formatted by running it with ``-c``.
  329. Any files that contain incorrectly sorted and/or formatted imports will be outputted to ``stderr``.
  330. .. code-block:: bash
  331. isort **/*.py -c -vb
  332. SUCCESS: /home/timothy/Projects/Open_Source/isort/isort_kate_plugin.py Everything Looks Good!
  333. ERROR: /home/timothy/Projects/Open_Source/isort/isort/isort.py Imports are incorrectly sorted.
  334. One great place this can be used is with a pre-commit git hook, such as this one by @acdha:
  335. https://gist.github.com/acdha/8717683
  336. This can help to ensure a certain level of code quality throughout a project.
  337. Git hook
  338. --------
  339. isort provides a hook function that can be integrated into your Git pre-commit script to check
  340. Python code before committing.
  341. To cause the commit to fail if there are isort errors (strict mode), include the following in
  342. ``.git/hooks/pre-commit``:
  343. .. code-block:: python
  344. #!/usr/bin/env python
  345. import sys
  346. from isort.hooks import git_hook
  347. sys.exit(git_hook(strict=True))
  348. If you just want to display warnings, but allow the commit to happen anyway, call ``git_hook`` without
  349. the `strict` parameter.
  350. Setuptools integration
  351. ----------------------
  352. Upon installation, isort enables a ``setuptools`` command that checks Python files
  353. declared by your project.
  354. Running ``python setup.py isort`` on the command line will check the files
  355. listed in your ``py_modules`` and ``packages``. If any warning is found,
  356. the command will exit with an error code:
  357. .. code-block:: bash
  358. $ python setup.py isort
  359. Also, to allow users to be able to use the command without having to install
  360. isort themselves, add isort to the setup_requires of your ``setup()`` like so:
  361. .. code-block:: python
  362. setup(
  363. name="project",
  364. packages=["project"],
  365. setup_requires=[
  366. "isort"
  367. ]
  368. )
  369. Why isort?
  370. ==========
  371. isort simply stands for import sort. It was originally called "sortImports" however I got tired of typing the extra
  372. characters and came to the realization camelCase is not pythonic.
  373. I wrote isort because in an organization I used to work in the manager came in one day and decided all code must
  374. have alphabetically sorted imports. The code base was huge - and he meant for us to do it by hand. However, being a
  375. programmer - I'm too lazy to spend 8 hours mindlessly performing a function, but not too lazy to spend 16
  376. hours automating it. I was given permission to open source sortImports and here we are :)
  377. --------------------------------------------
  378. Thanks and I hope you find isort useful!
  379. ~Timothy Crosley