liveBook · Manning

Dependency

As explained in Build Python Web Apps with Streamlit

An external dependency is software or a remote service outside a component's implementation that the component relies on for functionality.

A dependency is software or a service that a component relies on but does not implement itself. A simple to-do app might need only Python and Streamlit. Add a feature that fetches information online, and the app may also depend on a third-party library and an API. Those dependencies help the app do more, but they also affect deployment and testing. section 5.3 section 11.3.1

Two common kinds of external dependency are:

  • A Python library, such as requests: app code calls it, but its implementation lives outside the app. section 5.4.1 section 11.3.1
  • A remote service, such as an API that supplies dictionary entries, exchange rates, or news headlines: the app connects to it to obtain information. section 5.3

A dependency that works on your laptop must also be available in production. For Python libraries, list compatible versions in requirements.txt and install them with pip install -r requirements.txt. Streamlit Community Cloud reads that file from the GitHub repository and installs the listed libraries. An unqualified name leaves the version unspecified; == selects an exact version, while >= sets a minimum. Matching versions matters when code uses a feature absent from an older release, and even a newer release could remove a feature the app needs. section 5.4.1

Listing 5.5 A strict requirements.txt file
requests==2.31.0
streamlit==1.34.0

The strict requirements.txt example pins requests and streamlit to exact versions, making the intended production installations explicit. section 5.4.1

A virtual environment keeps a project's Python and libraries isolated, so changing one project's dependencies need not disrupt another. Remote services need different preparation: an app using an external API may require an account and a secret key, which must be kept safe. section 2.2.3 section 9.2.2

Keep a unit test independent of an API

Suppose a movie-search component calls requests.get, then uses the TMDB API's response. Both the requests implementation and the API are outside that component. Replace their behavior with controlled fakes during a unit test: the test can check the component's own code without making a real network call or failing because the API is busy. Use integration or end-to-end tests to examine behavior beyond that isolated component. section 11.3.1 section 11.8

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains library version management and dependency isolation in unit tests; leaves external-component failures to integration and end-to-end tests.
Examples
Uses a to-do app's requirements.txt and a movie-search component that calls requests and the TMDB API.
What it assumes
Introduces the dependency boundary in plain terms while assuming a Python app with imports and components to deploy or test.
What you can do after
Operate/configure Python dependencies for deployment and diagnose/debug component behavior by mocking external dependencies in unit tests.
How it connects
Connects dependencies to virtual environments, requirements.txt, API keys, mocking, and integration testing.

Also explained in

Compare how 27 books explain dependency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.