TFLearn
pip installation
bug
machine learning
Python libraries

TFLearn pip installation bug

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TFLearn is a high-level deep learning library built on top of TensorFlow. Installation via pip install tflearn frequently fails or produces runtime errors because TFLearn has not been updated to support TensorFlow 2.x. The core issue is that TFLearn relies on TensorFlow 1.x APIs (tf.Session, tf.global_variables_initializer, tf.contrib) that were removed or relocated in TensorFlow 2.0+. The practical solution is to either pin TensorFlow to a compatible 1.x version, use tf.compat.v1 mode, or migrate to a maintained alternative like Keras (now built into TensorFlow).

The Installation Error

bash
1$ pip install tflearn
2# Installs successfully, but then:
3
4$ python -c "import tflearn"
5# Various errors depending on TF version:
6# AttributeError: module 'tensorflow' has no attribute 'Session'
7# ModuleNotFoundError: No module named 'tensorflow.contrib'
8# AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'

The pip install itself succeeds because TFLearn's setup.py does not enforce a TensorFlow version ceiling. The errors appear at import time when TFLearn tries to call removed TF 1.x APIs.

Root Cause

TFLearn was designed for TensorFlow 1.x and uses APIs that were changed or removed in TF 2.0:

TFLearn UsesTF 2.x Status
tf.SessionRemoved (eager execution is default)
tf.global_variables_initializerRemoved
tf.contribRemoved entirely
tf.reset_default_graphRemoved
tf.placeholderRemoved

The TFLearn GitHub repository has had no significant updates since 2019, and the library is effectively unmaintained.

Fix 1: Pin TensorFlow 1.x (Virtual Environment)

Install a compatible TensorFlow version in an isolated environment:

bash
1# Create a virtual environment
2python3 -m venv tflearn-env
3source tflearn-env/bin/activate  # Linux/Mac
4# tflearn-env\Scripts\activate   # Windows
5
6# Install compatible versions
7pip install tensorflow==1.15.5
8pip install tflearn
9
10# Verify
11python -c "import tflearn; print('TFLearn imported successfully')"

TensorFlow 1.15.5 is the last 1.x release and works with Python 3.7 (not 3.8+). For newer Python versions, you need a different approach.

Fix 2: Use tf.compat.v1 Mode

Force TensorFlow 2.x to behave like 1.x:

python
1# Add this BEFORE importing tflearn
2import tensorflow.compat.v1 as tf
3tf.disable_v2_behavior()
4
5import tflearn
6# Now TFLearn can use tf.Session, tf.placeholder, etc.

Or set the environment variable before running your script:

bash
1# Set environment variable
2export TF_FORCE_GPU_ALLOW_GROWTH=true
3
4# Run with compatibility
5python -c "
6import tensorflow.compat.v1 as tf
7tf.disable_v2_behavior()
8import tflearn
9print('TFLearn loaded with TF2 compat mode')
10"

Limitations of Compat Mode

The tf.contrib module was completely removed in TF 2.x and is not available even in compat mode. TFLearn features that depend on tf.contrib (some RNN cells, batch normalization variants) will still fail.

Fix 3: Install from GitHub (Patched Forks)

Community-maintained forks may have partial TF 2.x compatibility:

bash
1# Install from the main repo (may have some patches)
2pip install git+https://github.com/tflearn/tflearn.git
3
4# Or a specific fork with TF2 patches (check for recent forks)
5pip install git+https://github.com/<user>/tflearn.git@tf2-compat

Always check the fork's commit history and issues to verify it works with your TensorFlow version.

Since TFLearn is unmaintained, migrating to Keras (built into TensorFlow 2.x) is the best long-term solution:

python
1# TFLearn code
2import tflearn
3
4net = tflearn.input_data(shape=[None, 784])
5net = tflearn.fully_connected(net, 128, activation='relu')
6net = tflearn.dropout(net, 0.5)
7net = tflearn.fully_connected(net, 10, activation='softmax')
8net = tflearn.regression(net, optimizer='adam',
9                         loss='categorical_crossentropy')
10model = tflearn.DNN(net)
11model.fit(X_train, y_train, n_epoch=10)
python
1# Equivalent Keras code (TF 2.x)
2import tensorflow as tf
3from tensorflow import keras
4
5model = keras.Sequential([
6    keras.layers.Input(shape=(784,)),
7    keras.layers.Dense(128, activation='relu'),
8    keras.layers.Dropout(0.5),
9    keras.layers.Dense(10, activation='softmax'),
10])
11model.compile(optimizer='adam', loss='categorical_crossentropy',
12              metrics=['accuracy'])
13model.fit(X_train, y_train, epochs=10)

Keras offers the same high-level API with active maintenance, GPU support, and full TensorFlow 2.x integration.

Dependency Resolution Issues

Sometimes pip cannot resolve compatible versions:

bash
1# Force reinstall with specific versions
2pip install --force-reinstall tensorflow==1.15.5 tflearn==0.5.0
3
4# If numpy conflicts arise
5pip install numpy==1.18.5 tensorflow==1.15.5 tflearn
6
7# Check what is installed
8pip list | grep -E "tensorflow|tflearn|numpy"

Common Pitfalls

  • Installing TFLearn without pinning TensorFlow: pip install tflearn pulls the latest TensorFlow (2.x), which is incompatible. Always specify pip install tensorflow==1.15.5 tflearn or use the compat mode approach.
  • Using Python 3.8+ with TensorFlow 1.15: TensorFlow 1.15 only supports Python 3.5-3.7. On newer Python versions, you must use TF 2.x with tf.compat.v1 mode or use Docker with an older Python image.
  • Expecting tf.compat.v1 to restore all TF 1.x functionality: While compat mode restores tf.Session and tf.placeholder, it does not restore tf.contrib, which was removed entirely. TFLearn features depending on tf.contrib will still break.
  • Not using a virtual environment: Installing TFLearn globally can break other projects that depend on TensorFlow 2.x. Always use a virtual environment (venv or conda) to isolate TFLearn's dependencies.
  • Investing in TFLearn for new projects: TFLearn is unmaintained (last meaningful update in 2019). New projects should use Keras (tf.keras), PyTorch, or another actively maintained framework instead.

Summary

  • TFLearn fails with TensorFlow 2.x because it uses removed APIs like tf.Session and tf.contrib
  • Pin tensorflow==1.15.5 in a virtual environment for the most reliable fix
  • Use tf.compat.v1 with disable_v2_behavior() for a TF 2.x workaround (partial compatibility)
  • Migrate to tf.keras for a maintained, high-level API that replaces TFLearn's functionality
  • TFLearn is effectively unmaintained — avoid it for new projects

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.