# Untangling our Python Code

DevFeed: [Untangling our Python Code](<https://devfeed.tech/articles/untangling-our-python-code-27401.md>)

Original publisher: [Read original article](<http://engineering.khanacademy.org/posts/python-refactor-3.htm>)

Author: Khan Academy

Published: 2018-04-16T22:00:00Z

Content type: article

Language: en

Sources: [Khan Academy](<https://devfeed.tech/sources/khan-academy.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [project layout](<https://devfeed.tech/topics/project-layout.md>), [import](<https://devfeed.tech/topics/import.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>)

Tags: [2017](<https://devfeed.tech/tags/2017.md>), [2018](<https://devfeed.tech/tags/2018.md>), [a-b-testing](<https://devfeed.tech/tags/a-b-testing.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [code](<https://devfeed.tech/tags/code.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [dependency-graph](<https://devfeed.tech/tags/dependency-graph.md>), [design](<https://devfeed.tech/tags/design.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [infrastructure](<https://devfeed.tech/tags/infrastructure.md>), [news](<https://devfeed.tech/tags/news.md>), [python](<https://devfeed.tech/tags/python.md>), [refactor](<https://devfeed.tech/tags/refactor.md>)

## AI overview

Khan Academy reduced tangled Python imports by organizing code into 40 top-level packages with a mostly linear dependency order: packages may import from lower levels, while imports from higher levels count as bad dependencies. A script produced HTML reports to identify violations; developers then moved misplaced functions or refactored poorly organized code, such as moving coach-specific experiment logic out of `flags/experiments.py`. The share of bad inter-package dependencies fell from 15.2% to 7.8%, though about 750 violating imports remained whitelisted. A commit-time check flags newly added bad imports unless explicitly allowed. The article says the structure made code easier to locate and reason about, while acknowledging that the linear order sometimes forced arbitrary ordering between unrelated packages.

## Source excerpt

By Carter Bastian The previous posts about The Great Khan Academy Python Refactor of 2017 and Also 2018 ... Read more