# Fix "AttributeError: module 'tensorflow' has no attribute 'Session'" in TensorFlow 2

An agent gets TensorFlow 1 graph code running on TensorFlow 2 through the compat.v1 API, clears the errors queued behind tf.Session, and runs a short training pass.

[Run this on Rush](https://cloud.getrush.ai/run/fix-tensorflow-session-attribute-error/tensorflow/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

TensorFlow 2.0 made eager execution the default and moved the graph-and-session API out of the top-level namespace. `tf.Session`, `tf.placeholder`, `tf.variable_scope` and their relatives still exist, under `tf.compat.v1`. Code written for 1.x fails with `AttributeError: module 'tensorflow' has no attribute 'Session'`, and the common fix from search results is to edit that one line to `tf.compat.v1.Session()`. That moves the failure to the next 1.x name. The agent treats the file as a unit: it switches the whole module to the compatibility API, turns v2 behaviour off, and keeps going until a training step has actually run.

kvfrans/variational-autoencoder is a typical target, a few files from a 2016 blog post that people still clone to learn from. `main.py` builds its graph in the class constructor with `tf.placeholder`, `tf.random_normal`, `tf.log` and `tf.train.AdamOptimizer`, and `ops.py` adds `tf.variable_scope` and `tf.get_variable`. The `with tf.Session() as sess:` line sits in `train()`, followed by `tf.initialize_all_variables()`. Because the constructor runs first, `placeholder` raises before `Session` is ever reached. The agent changes the import in `main.py`, `ops.py` and `utils.py` to `import tensorflow.compat.v1 as tf`, calls `tf.disable_v2_behavior()` once at the top of `main.py`, and leaves the graph-building lines as they are.

Two older layers sit in front of TensorFlow here, and the agent has to get through them to prove anything. The code is Python 2: `print` statements in `main.py` and `input_data.py`, `xrange`, and `urllib.urlretrieve`. In `utils.py`, `merge` computes `j = idx / size[1]` and uses it in a slice, which under Python 3 is a float and raises a `TypeError`, so it becomes `//`. And `main.py` imports `imsave` from `scipy.misc`, which SciPy removed; the agent replaces it with `imageio.imwrite` and converts the float image to 8-bit first, since `imsave` used to rescale silently.

Then it runs the thing. `input_data.py` downloads MNIST from a URL hard-coded in `SOURCE_URL`; the agent checks whether that address still serves the four files and, if it does not, reports that and reads the same files from a mirror it names in the pull request. `train()` loops for ten epochs, which is more than a smoke test needs on a CPU, so the agent runs a bounded number of batches through a temporary copy of the loop, confirms the two losses are finite numbers and that a sample grid is written, and discards the checkpoint and image files so the committed `results/` pictures are untouched. It does not rewrite the model for Keras.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project was written for TensorFlow 1.x and fails on TensorFlow 2 with \"AttributeError: module 'tensorflow' has no attribute 'Session'\" (or 'placeholder', 'variable_scope', 'get_variable', 'global_variables_initializer'). Make it run on the newest TensorFlow 2 release that still provides tf.compat.v1, without rewriting the model. In every module that builds or runs the graph, import tensorflow.compat.v1 as tf, and call tf.disable_v2_behavior() once, before any graph is built. Do not edit individual tf.Session lines one at a time. List every 1.x symbol the code uses and confirm each exists under compat.v1; for any that does not (tf.contrib is the usual case), name the replacement and make the smallest change that preserves behaviour. If the code is Python 2 (print statements, xrange, urllib.urlretrieve, integer division used as an index), port those lines to Python 3 in a separate commit. Replace imports that current SciPy or NumPy no longer have, and keep the saved output equivalent. Then run it: if the script downloads a dataset, check the URL responds and report if it does not; if training is long, run a bounded number of steps through a temporary copy of the loop rather than changing the committed hyperparameters. Show that the loss values are finite and that the script writes the outputs it is meant to. Do not commit checkpoints, datasets or regenerated images. Add a requirements file with the versions you ran. In the pull request, list the symbols table, the Python 3 changes, the commands and their output, and say what a full migration to native TF2 would involve without doing it.",
  "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 with the Python 3 port and the TensorFlow change as separate commits. The second is small by design: three import lines and one call to `tf.disable_v2_behavior()`. The description has a table of every 1.x symbol found in the code, each marked as present under `compat.v1` or given a replacement. It also carries the three non-TensorFlow fixes with a line of explanation each: the integer division in `merge`, the `urllib` call and the image writer.

The run section shows the command, the TensorFlow and Python versions, the loss values printed for the bounded run and the name of the sample image it produced. A reviewer checks that no checkpoint, dataset or JPEG entered the diff, and reads the image-writer change with care, because the old `imsave` rescaled values to 0 through 255 and a replacement that skips that step writes near-black pictures without raising anything. The closing paragraph describes what moving to native TensorFlow 2 would take, such as replacing placeholders with function arguments and the hand-written batch normalisation with a Keras layer, so the maintainer can decide whether that is worth a second run.

## Run it on every new GitHub issue

Tutorial repositories get this error reported again and again by readers, each opening an issue with the same traceback. In the console, add an automation to the Project with the prompt above and a `github` trigger on `issues.opened`. Rush starts a run with the saved prompt and adds the issue after it as the event. Add a sentence to the prompt saying to act only when the issue text contains `has no attribute 'Session'` or one of the sibling names, and otherwise to answer in one line.

The trigger fires only for issues opened by an owner, member or collaborator of the repository, so a stranger's report does not start a run by itself. A maintainer who wants the agent on such a report adds a label, and a second trigger on `issues.labeled` with that name in `labels` starts the run when they do. Turn on the automation's read-only setting if you want a comment with the diagnosis and no branch pushed.

## Why run it on Rush?

TensorFlow is a large install with narrow rules about which Python it supports, and a laptop that already has one version for current work is a poor place to try another. The agent creates a fresh environment in its sandbox, installs the release it has chosen and runs the script there. Every command and its output is kept in the session transcript, including the pip resolver's choices, so the versions in the new requirements file are the ones that ran.

The proof here is a training loop that touches the network, the disk and the CPU for several minutes. In the sandbox the dataset download, the checkpoint directory and the generated images all land on a disk that belongs to the run, and the agent removes them before committing. If the bounded run fails at batch three with a shape error, that output is in the transcript too, and the pull request says so instead of claiming success from an import that worked.

## Frequently asked questions

### Is changing tf.Session() to tf.compat.v1.Session() enough?

Rarely. The session is one of many 1.x names, and with v2 behaviour still on, placeholders and variable scopes misbehave even when spelled with `compat.v1`. Importing `tensorflow.compat.v1 as tf` and calling `tf.disable_v2_behavior()` handles the whole module at once, which is why the agent does that.

### Will a model trained under 1.x load after this change?

A checkpoint written by `tf.train.Saver` in 1.x can be restored by `tf.compat.v1.train.Saver` when the graph defines the same variable names. The agent leaves names and scopes alone for that reason. If the repository includes a checkpoint, the prompt can ask it to restore it and report whether every variable matched.

### What about tf.contrib?

`tf.contrib` was removed outright and has no `compat.v1` equivalent. Each use needs its own replacement: some moved into core TensorFlow, some into separate packages, and some were dropped. The agent lists every `tf.contrib` symbol it finds with the replacement it chose, and stops to report when one has none.

### Why not migrate to native TensorFlow 2 while it is in there?

Because that is a rewrite of how the model is defined and trained, and a reviewer cannot easily tell whether a rewritten model is the same model. The compatibility route keeps the graph code line for line, so the diff is reviewable. The pull request ends with a description of the full migration so it can be a separate, deliberate job.

## More jobs like this: Fix a bug from an alert

- [Fix ClientClosedError: The client is closed in a Node.js Redis App](https://getrush.ai/run/fix-the-client-is-closed-error/node-redis/)
- [Fix "DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead" in a Python Library](https://getrush.ai/run/fix-the-warn-method-is-deprecated-use-warning-instead/logging/)
- [Fix "AttributeError: 'Thread' object has no attribute 'isAlive'" on Python 3.9+](https://getrush.ai/run/fix-thread-isalive-attribute-error/threading/)
- [Fix "AttributeError: module 'time' has no attribute 'clock'" on Python 3.8+](https://getrush.ai/run/fix-time-clock-attribute-error/time/)
- [Fix "_pickle.UnpicklingError: Weights only load failed" from torch.load in PyTorch 2.6 and Later](https://getrush.ai/run/fix-torch-load-weights-only-unpickling-error/pytorch/)
- [Fix "AttributeError: module 'tornado.web' has no attribute 'asynchronous'" after a Tornado upgrade](https://getrush.ai/run/fix-tornado-web-asynchronous-attribute-error/tornado/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
