Configuring virtualenv using Paramiko SSH

I have limited experience with Python and Django on Windows, and now I'm trying to figure out how to deploy my code to Ubuntu 16.04 LTS VPS. After reading various tutorials and lots of answers on SE, I managed to get pretty far (well, for me), but now I'm stuck.

In manual mode (via Putty), I can do the following:

# check that Python 3.5 is installed
python3 --version  
# install pip
sudo -kS apt-get -y install python3-pip  
# upgrade pip to newest version
pip3 install --upgrade pip
# check result
pip3 --version  
# install venv
sudo -kS pip3 install virtualenv virtualenvwrapper 
# create venv
virtualenv ~/Env/firstsite  
# make sure venv is created 
ls -l ~/Env/firstsite/bin/python  # /home/droplet/Env/firstsite/bin/python3.5 -> python3
# switch on venv
source ~/Env/firstsite/bin/activate  # (firstsite) droplet@hostname:~$
# check that python3 is taken from venv
which python3  # /home/droplet/Env/firstsite/bin/python3

So, the virtual environment is correctly created and enabled. I could continue to install Django.

However, when I try to do the same thing automatically, using Paramiko (I execute the commands with paramiko.SSHClient().exec_command(cmd, input_string, get_pty=False), everything goes exactly the same until the last command:

exec_command('which python3')

returns /usr/bin/python3. Therefore, I assume that it source activatedoes not work through Paramiko SSH.

  • Why?
  • How can I deal with it?
  • , venv ( ) ?
+4
1

@Pablo Navarro : virtualenv Bash script ( paramiko ssh).

exec_command python , :

stdin, stdout, stderr = ssh.exec_command(/path/to/env/bin/python script.py)

( miniconda env, pyODBC):

stdin, stdout, stderr = ssh.exec_command(~/miniconda2/envs/pyODBC/bin/python run_script.py)

~/miniconda2/envs/pyODBC/bin/python -m pip list env

0

Source: https://habr.com/ru/post/1650324/


All Articles