All jobs

Fix "ModuleNotFoundError: No module named 'plotly.plotly'" after plotly 4

Yes. An agent removes or redirects the plotly.plotly import, moves make_subplots to plotly.subplots, and runs the script on the plotly a fresh install resolves to.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

plotly 4.0.0, released in July 2019, moved everything that talked to the Chart Studio cloud out of the package. import plotly.plotly as py kept working in 4.x and 5.x only as a shim that raises ImportError: The plotly.plotly module is deprecated, please install the chart-studio package and use the chart_studio.plotly module instead. (the wording on 5.24.1). On the current release, 7.1.0, the shim is gone and the same line raises ModuleNotFoundError: No module named 'plotly.plotly'. Code from 2017 and 2018 that never pinned plotly meets one of the two on its first fresh install.

Cadene/vqa.pytorch, the reference code for the MUTAN visual question answering paper, is a clear case. Its requirements.txt lists numpy, torch, h5py, pyyaml, colorlover, plotly, scipy, nltk and click, none pinned. visu.py, which the README's Monitor training section runs as python visu.py --dir_logs logs/vqa/mutan_noatt, opens with import plotly.plotly as py, import plotly.graph_objs as go, from plotly.offline import download_plotlyjs, plot and from plotly import tools. The name py is never used again in the file: both plotting functions build go.Scatter traces, lay them out with tools.make_subplots, and write an HTML file with plot(fig, filename=path_visu, auto_open=auto_open) from the offline module. On plotly 7.1.0 the script stops at line 9, before it reads a single log, with the ModuleNotFoundError above.

The agent reproduces that first, on a clean environment with the versions a new user gets, then reads what each plotly import is for. An import that nothing uses is deleted. A py.plot or py.iplot call that uploads to Chart Studio, which this file does not have, becomes chart_studio.plotly with chart-studio added to the requirements, or an offline plot, and the pull request says which and why. plotly.tools.make_subplots still exists on 7.1.0 but warns plotly.tools.make_subplots is deprecated, please use plotly.subplots.make_subplots instead, so the two calls move to plotly.subplots.make_subplots, which takes the same rows, cols and subplot_titles arguments. plotly.offline.plot and download_plotlyjs are unchanged.

Then it runs the script to the end. visu.py reads logger.json from an experiment directory through Experiment.from_json in vqa/lib/logger.py and the per-epoch accuracy files beside it, so the agent writes a small experiment directory with a few epochs of numbers, runs python visu.py --dir_logs against it, and checks that visu.html is written and opens as a two-panel figure. It does not need torch for that path: the script's own imports are numpy, colorlover and plotly, and the vqa.lib.logger module imports only the standard library and numpy.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This repository fails with \"ModuleNotFoundError: No module named 'plotly.plotly'\" (or \"ImportError: The plotly.plotly module is deprecated\") after plotly was installed fresh. Read the dependency files and report how plotly is pinned, then create a clean virtual environment, install the dependencies as a new user would, and report the plotly version that was resolved. Run the script that fails and save the traceback. Search every .py and .ipynb file for plotly.plotly, py.plot, py.iplot, py.sign_in and plotly.tools and list each use with its file and line. Delete imports of plotly.plotly that nothing uses. Where code actually uploads to Chart Studio, decide between chart_studio.plotly (adding chart-studio to the requirements) and an offline plot, and explain the choice. Move plotly.tools.make_subplots to plotly.subplots.make_subplots. Do not pin plotly below 4 to hide the error. Run the script again against a small example input you construct, confirm it writes its HTML output, and keep going until it completes, listing any further removed API it hits. Open a pull request with the resolved versions, the list of changed lines, and the command that now succeeds.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request whose diff is a few lines in one script: an import deleted, from plotly import tools replaced by from plotly.subplots import make_subplots, and the two tools.make_subplots( calls renamed. The description lists the plotly version a clean install resolved, the traceback before the change, every plotly.plotly and plotly.tools use found in the repository, and the python visu.py --dir_logs command that now writes visu.html.

What a reviewer looks at is the evidence that the figure is the same figure. Removing an unused import cannot change output, but the subplot call moved modules, so the description names the arguments it passes (rows, cols, subplot_titles) and states that the new function accepts them unchanged, and the HTML it produced from the example logs is attached to the session as an artifact for a look.

If a script did upload to Chart Studio, the pull request says so and shows the choice: chart-studio added to the requirements with import chart_studio.plotly as py, or the call rewritten as an offline plot. Either is a separate commit from the import cleanup, so a reviewer can take the cleanup and discuss the rest.

Run it on every Monday morning

A repository with unpinned dependencies breaks without a commit, so there is no push to hang a check on. Give the Project an automation with a schedule trigger, for example the cron expression 0 7 * * 1 in your timezone, and a prompt that tells the agent to install the dependencies fresh, run each plotting or monitoring script against a small example input, and open a pull request only when one fails on an API a new release removed. On a week when nothing broke, the run ends with a one-line answer and no pull request.

For a group of research repositories that share this 2018 plotly shape, start the request above once per repository from a script with an API key, and read each session's pr_url when its run.completed webhook arrives. Each run resolves its own versions in its own sandbox, and runs past the key's max_concurrent limit are accepted and wait as queued.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The edit is small. The question worth a run is whether the script still produces its figure on a plotly three major versions newer than the one it was written for, and answering it means a clean environment, an example experiment directory, a full run and a look at the HTML. An agent in a sandbox does all four, and the commands and their output stay in the session, so the pull request arrives with the run already made.

It also starts from the state your users are in. A laptop that has had plotly 3 installed since the paper came out never shows this error. A sandbox has no history: the install resolves what a new clone gets today, which is the only environment where the import fails.

Frequently asked questions

My traceback says ImportError and mentions chart-studio, not ModuleNotFoundError. Same problem?

Yes. plotly 4.x and 5.x kept a plotly.plotly shim whose only job was to raise that ImportError pointing at the chart-studio package; 7.1.0 has no shim, so the import is a ModuleNotFoundError. Which one you see depends only on the plotly version a fresh install resolved.

Can I pin plotly below 4 instead?

plotly 3.10.0 still installs and imports plotly.plotly, so a pin works for a while. But the Chart Studio upload it provided is a cloud service that needs an account and a key, and every other plotly feature the script uses is offline, so the pin keeps a dependency on a service the code never calls. The request above tells the agent not to pin.

Does the agent also fix plotly.tools.make_subplots?

Yes. On 7.1.0 the old function still runs but warns that it is deprecated in favour of plotly.subplots.make_subplots, which takes the same rows, cols and subplot_titles arguments. The agent moves the calls in the same pull request so the next plotly release does not break the script again.

Does the agent need torch or the dataset to check the fix?

Not for this script. visu.py imports numpy, colorlover, plotly and the repository's logger module, which uses only the standard library and numpy. The agent builds a small experiment directory with a logger.json and a few epoch folders and runs the script on that.

What if the script really uploads with py.plot?

Then the agent decides between chart_studio.plotly, which needs the chart-studio package and a Chart Studio account, and the offline plotly.offline.plot, which writes the same figure to an HTML file. It makes that a separate commit and explains the choice in the pull request so you can decide.

More jobs like this: Fix a bug from an alert

Keep reading