Tensorflow Virtualenv PyCharm

green leaves
green leaves

If so, this is a way to combine both, your tensorflow code running in a virtual environment used by PyCharm and from command line.

This article assumes you installed Python3 and pip3 already.

First step would be to create a new virtual environment using PyCharms Project Interpreter dialog for your existing project:

PyCharm Project Interpreter Dialog
PyCharm Project Interpreter Dialog

The settings symbol on the left upper corner allows you to initiate a new virtual environment creation:

PyCharm Project Interpreter Dialog - create new virtual environment
PyCharm Project Interpreter Dialog – create new virtual environment

As in the tensorflow installtion guide you should inherit global site-packages:

PyCharm Project Interpreter Dialog - create new virtual environment sub dialog
PyCharm Project Interpreter Dialog – create new virtual environment sub dialog

After the virtual environment is created, I did test it first from using command line:

# optional
$ cd ~/tensorflow_pycharm
# activate the virtual env
$ source ~/tensorflow_pycharm/bin/activate
# deactivate the virtual env after your prompt should have changed
(tensorflow)$ deactivate
virtual env comand line first test
virtual env comand line first test

Now its time to define & install the correct tensorflow binary to install as defined in the tensorflow installtion guide. In my case it would be:

$ cd ~/tensorflow_pycharm
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0rc0-cp34-cp34m-linux_x86_64.whl
$ sudo pip3 install --upgrade $TF_BINARY_URL
tensor flow virtual environment installation
tensor flow virtual environment installation

This virtual environment can then be used in a PyCharm project run configuration:

PyCharm Run Edit Configurations
PyCharm Run Edit Configurations
new python configuration
new python configuration

Select the just created virtual environment in case it is not preselected yet:

default python configuration
default python configuration

Set the script to be run. The intro tensorflow script (from tensorflow introduction) is used for this post:

script to run
script to run
run the virtual environment configuration
run the virtual environment configuration

No you are ready to run your script via virtual environment from both, PyCharm and command line:

  • PyCharm:
    run script using PyCharm virtual env
    run script using PyCharm virtual env
  • command line:
    run script using command line virtual env
    run script using command line virtual env

related links:

6 Comments

    • I am not sure if I understand your question. Tensorflow functionality can be used in python code by importing the tensorflow module. Would that answer your question?

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.