我在 Windows 中使用 Python 和 Django 的经验有限,现在我试图了解如何将我的代码部署到 Ubuntu 16.04 LTS VPS。在阅读了有关 SE 的各种教程和大量答案后,我成功地取得了很大的进展(好吧,对我来说),但现在我陷入了困境。
手动(通过 Putty)我可以执行以下操作:
# 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
这样虚拟环境就已经正确创建并开启了。我可以继续安装 Django。
然而,当我尝试在自动化机制中使用 Paramiko 执行完全相同的操作时(我使用以下命令执行命令)paramiko.SSHClient().exec_command(cmd, input_string, get_pty=False
),一切都以完全相同的方式进行,直到最后一个命令:
exec_command('which python3')
returns /usr/bin/python3
。所以我假设source activate
无法通过 Paramiko 的 SSH 工作。
- Why?
- 我该如何应对?
- 我可以检查 venv 是否以某种更直接(且可靠)的方式启用?
我们可以轻松激活 virtualenv 并在其上执行命令。
Example:
import paramiko
hostname = 'host'
port = 22
username = 'root'
password = 'root'
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password)
command = 'source /root/Envs/env/bin/activate;python3 --version;qark;echo hello'
(stdin, stdout, stderr) = s.exec_command(command)
for line in stdout.readlines():
print(line)
for line in stderr.readlines():
print(line)
s.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)