TensorFlow projects often need a Python package set that can be upgraded, rebuilt, or removed without changing other machine-learning work on the same computer. A dedicated venv environment keeps the project interpreter entry points and pip packages together while still using Python's standard library tooling.
The venv module builds the new environment from the exact Python executable used to start it. Choose a TensorFlow-compatible executable before creation, because the environment will keep using that interpreter line after activation.
The shell is ready for TensorFlow installation when python and pip resolve inside ~/venvs/tf. Checking pip through python -m pip also avoids accidentally calling a user-level or system-level pip command with the same name.
$ python3.12 --version Python 3.12.13
TensorFlow 2.21 wheels are published for CPython 3.10 through 3.13. Substitute another supported executable, such as python3.13, when that is the version installed on the workstation.
$ mkdir -p ~/venvs
$ python3.12 -m venv ~/venvs/tf
The venv module creates ~/venvs/tf/bin/python and a private site-packages directory without modifying the system Python installation.
$ source ~/venvs/tf/bin/activate (tf) $
The prompt usually shows the environment name after activation. Open a new shell or run the activation command again before later TensorFlow work.
(tf) $ python -m pip --version pip 25.0.1 from /home/user/venvs/tf/lib/python3.12/site-packages/pip (python 3.12)
The important path segment is /venvs/tf/lib/python3.12/site-packages. A system path or user-site path means the shell is not using the intended environment.
(tf) $ python -m pip install --upgrade pip Requirement already satisfied: pip in /home/user/venvs/tf/lib/python3.12/site-packages (25.0.1) Collecting pip ##### snipped ##### Successfully installed pip-26.1.2
TensorFlow's install documentation recommends a recent pip before installing the package. This upgrade changes only ~/venvs/tf.
(tf) $ python -m pip --version pip 26.1.2 from /home/user/venvs/tf/lib/python3.12/site-packages/pip (python 3.12)
Install TensorFlow from this active shell so the package lands in the environment that produced this path.
Related: How to check the TensorFlow version
(tf) $ deactivate $
Skip deactivation when the next command will install TensorFlow into the same environment.