Using a virtual environment on a remote machine

Answered

Running Pycharm on Mac OS X. Developing on a Raspberry PI. I can create and use a remote interpreter on the Pi over SSH. I would like to use a remote environment on the Pi (venv-3.4 with Python 3.4). How do I create a remote interpreter in Pycharm, that uses a remote environment?

 

Thanks,

Martin

13 comments

Hello Martin,
Creating remote virtualenv from PyCharm is not supported yet: http://youtrack.jetbrains.com/issue/PY-6002. But you can just create it on the server manually and add it as a regular remote interpreter.

0

How do you activate the venv for PyCharm remote execution once it is created? 

PyCharm 2017.3.2 (Professional Edition)
Build #PY-173.4127.16
SSH Remote Run v0.1 confirmed active

I don't see any options for selection and activation of remote venv on either

  • Build, Execution, Deployment > Deployment
  • Build, Execution, Deployment > Deployment > Options

Setting the Python interpreter path in Project: name-of-project > Project Interpreter does not set up the environment variables with activate. As a result, it seems you can only get the Python binary that resides at a path that happens to be in a venv, but would not provide the paths required to use the contents of the venv. Not to mention that this dialog ends up being completely non-functional, giving "Project level deployment servers are not currently supported. Please create one using any of the two links above." Neither of the links have any apparent action.

See also: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206599545-How-to-set-a-remote-virtualenv-as-the-python-interpreter-

5

Hi. I found a solution which works (implements remote virtualenv interpreter in PyCharm!)

1. Create script (e.g. /usr/bin/python36fortytwo)

#!/bin/bash
source /home/pavel/.virtualenvs/fortytwo/bin/activate
# now we call python which should be python from our virtualenv
python "$@"

2. chmod +x /usr/bin/python36fortytwo

3. Create remote interpreter specifying /usr/bin/python36fortytwo as command

You can use it in Run configuration and list/deploy packages

Run the project and you see it running:

 

ssh://root@192.168.10.75:22/usr/bin/python36fortytwo -u /opt/dev/fortytwo/manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

September 06, 2018 - 19:53:00
Django version 2.1.1, using settings 'fortytwo.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

1

Perhaps I am missing something but what prevents you from selecting /home/pavel/.virtualenvs/fortytwo/bin/python as a Python interpreter path? PyCharm will correctly resolve venv specific packages.

0

I am new to python and virtualenv, so if I am not sure what is done in background, I do it the way which leaves less possibility for errors :)

e.g. I compared environment variables before ant after activating "fortytwo" virtualenv, so two things get set in environment

VIRTUAL_ENV=/home/pavel/.virtualenvs/fortytwo
PATH=/home/pavel/.virtualenvs/fortytwo/bin:$PATH

making virtualenv's /bin directory first on PATH makes it be the first to search for "pip" for example

Maybe in PyCharm context this doesn't matter (e.g. it calls "python -m pip" and not just "pip"), maybe somewhere it does, I don't know for sure, but this way makes sure it will work same as it would for user on that machine

 

Of course the better way is to put file venv-python.sh in every virtualenv directory with content:

 

#!/bin/bash
source bin/activate
python "$@"

and the give it to PyCharm as /home/pavel/.virtualenvs/fortytwo/venv-python.sh

so it

1. doesn't live in OS bin directories
2. doesn't have virtualenv/project specific content in it.

 

0

@Pavel, @Anna

No as far as I can see it *does not work*. Of course I can select the python executable in the remote virtualenv, but then all the packages that are *only* installed in the virtualenv will be missing.

Please prove me wrong!

0

@Stefan Here is my example, pytest is installed only for the virtual environment:

1

That seems to work fine.  Here are my steps:

```

sudo apt-get install --upgrade python3-dev python3-venv
python3 -m venv MYENV
MYENV/bin/pip install -U pip setuptools wheel
source MYENV/bin/activate
pip3 install -U smbus2

```

/c

0

I had the same issue and solved it also with selecting the python interpreter from the virtual environment. In my case

/root/.virtualenvs/cv/bin/python3

I faced also the problem to run a script to set some env variables for qt5. With the solution from Pavel it's now possible for me to use X11 features. Thank you! 

0

On Cloud (EC2 instance e.g.,)

$ source activate tensorflow2dot7

(tensorflow2dot7) ubuntu@ip-172-31-88-172:/usr/bin$ python

Python 2.7.16 |Anaconda, Inc.| (default, Mar 14 2019, 21:00:58) 

>>> import sys

>>> print(sys.executable)

/home/ubuntu/anaconda3/envs/tensorflow2dot7/bin/python

-----------

Then use this "/home/ubuntu/anaconda3/envs/tensorflow2dot7/bin/python" in Pycharm Interpreter

 

 

0

[Connecting to Ubuntu 18.04 with Python 3.6 from PyCharm Professional 2019.2]

Sadly, pointing the interpreter to the python executable in the venv does not solve the issue of missing modules, I still have to use @Pavel Paidem's solution:

File: run-with-venv.sh

#!/bin/bash
source <path-to-venv>/bin/activate
python "$@"

Setting this file as interpreter works without issue, but is not the desired way of achieving it, as some people have pointed out that it should work natively.

0

@Fniehaus

Please feel free to vote for https://youtrack.jetbrains.com/issue/PY-6002 and leave comments.

0

Please sign in to leave a comment.