Sharing a virtual environment between Anaconda and PyCharm

Pierre-Yves Dumas
4 min readDec 18, 2021

Notebooks are great to experiment, but code that goes into production generally comes from an IDE like PyCharm, so you want to make sure that you can run all your code in a single environment if you wish so. For instance we may want to create a virtual environment for TensorFlow 2.7 that is available for a runtime in PyCharm and also for a Jupyter notebook that runs on top of Anaconda. Here’s how to do it.

Create a TF27 virtual environment with Anaconda

Since I recently covered how to install TensorFlow GPU on Windows 10, I’ll keep using Windows here as a reference.

Get Anaconda here. You may skip ading Anaconda3 as the system Python 3.9 because we only care about Python 3.8 for TensorFlow 2.7.

Then open the Anaconda Navigator. You’re offered to open JupyterLab or a Jupyter Notebook. On the top left, you can see that you’re running on a “base” environment. Select “Environments” in the left menu to create a new TF27 environment.

Mind the chosen version of Python which has to be 3.8 for TensorFlow 2.7. You may also want to change where the virtual environment is created. To do so, use the Anaconda Prompt. From the Windows Start Menu, type “prompt” to find it, open it, then navigate to where you want to create your virtual environment, then use a command line.

The newly created virtual environment comes with a few utilities.

If you want to add a package with a specific version, use Anaconda Prompt to do so for now, remember to activate the right environment first:

Use the TF27 virtual environment with Jupyter

Now if you go back to the “Home” of the Anaconda Navigator and select the new virtual Environment, you can install Jupyter Notebook for it. Launching it from the Anaconda Navigator is not necessarily convenient though because of where it opens. A more convenient way is again with the Anaconda Prompt, open it, activate your new virtual environment, navigate where you want to create your notebook and then launch Jupyter Notebook.

Similarily, you may want to install JupyterLab for your new environment and launch it:

If you want to add a package with a specific version, use another Anaconda Prompt to do so, and remember to activate the right environment first. You can check that numpy is available in your notebook with a simple:

import numpy as np
print(np.array([1, 2, 3]))

Use the TF27 virtual environment with PyCharm

Now you need to add TensorFlow 2.7 to this virtual environment. You could do it with the Anaconda Navigator, but let’s do it in PyCharm. Open a project with PyCharm, you may already have a default Python Interpreter that you can see with Ctrl+Alt+S.

We want the interpreter to be based instead on the one we created with Anaconda, so add a new one. Choose on the left for it to be based on a Conda Environment. Choose this environment to be existing. The interpreter is the python.exe in the virtual environment you already created. The conda executable is the _conda.exe where you installed Anaconda. You can make it available to all projects.

Mind the Anaconda icon at the top of the list of the installed packages, click on it to use the Conda Package Manager. Add TensorFlow and specify the exact version, like 2.7.0, that you need. This may take a while.

The new package should also appear in the Anaconda Navigator.

Conclusion

You’re now ready to use PyCharm while retaining the ability to experiment with notebooks, and all within a single virtual environment that you fully control. Next we will discuss how we can use Python in PyCharm with the kind of visuals that we can easily enjoy in a notebook.

--

--