📜 ⬆️ ⬇️

Remote launch in PyCharm Community Edition

PyCharm is the most convenient, in my opinion, IDE for Python from the authors of the magnificent PhpStorm. Unlike the PHP development tool, it has a free version with a slightly reduced functionality, in particular, without a smart module for running and debugging scripts on a remote server. Nevertheless, the standard features are sufficient for creating desktop windows applications, for scripting, and for server code.

This feature became critical at that moment when I wanted to write scripts on a PC and get the result of their execution on Raspberry Pi without copying and running manually. Then my recipe for Windows 8.1 (just run).

Yes, let's go the hard way and use the machine running Windows as a workplace. It would have been easier to turn it on Linux, but at the same time I decided to look at the possibilities of a new Windows PowerShell for me instead of bash. Surprisingly, he managed. You can also use cmd.exe and bat files.
')
So, iron - Raspberry Pi any model with raspbian on board, access to the local network and running ssh. A PC with windows is connected to the same network, PyCharm and Python are already installed on it (at the time of writing, the current version in the Raspbian repositories is 3.2, it is better to install the same).

Communications


Microsoft's OS does not have regular ssh clients, so we put something convenient on it, like Xshell: www.netsarang.com/download/down_xsh.html . I also needed command line utilities from putty: pscp and plink: www.chiark.greenend.org.uk/~sgtatham/putty/download.html . By the way, pytty itself is also quite convenient.

Python on board


I will execute scripts as root. You can't do that.
We turn on the client (Xshell), connect to raspberry, set
apt-get install python3 

Fake interpreter


First, create a fake interpreter. We start Windows PowerShell on behalf of the administrator, we create the daddy.

 cd \ mkdir PythonWrapper cd PythonWrapper 

In order not to lose and not litter in PATH, we drop pscp and plink here:

 cp ~\Downloads\plink.exe . cp ~\Downloads\pscp.exe . 

PyCharm accepts only files with the name python.exe as an executing interpreter file, so let's do the following bad thing:

 PS C:\PythonWrapper> cmd.exe /c mklink python.exe C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe     python.exe <<===>> C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 

You may have to disable script signature checks:

 Set-ExecutionPolicy Unrestricted 

Create a script file for uploading the project to the server, for example this one (let it be called deploy.ps1):

 C:\PythonWrapper\plink -ssh root@192.168.1.230 -pw passw0rd 'rm -rf '$args[1]';mkdir -p '$args[1] $dpath = 'root@192.168.1.230:~/' + $args[1] + '/' C:\PythonWrapper\pscp -r -scp -pw passw0rd $args[0] $dpath 

Where 192.168.1.230 is the address of the remote server, root and passw0rd are the login and password. Hardcode is enough for once.
This version takes the address to the local folder and the name of the folder you want to create on the server, and simply copies all the content from the first to the second. For projects more than a hundred kilobytes worth optimizing this place.

Create a file transfer and run the code on the server (let python.ps1). In the simplest case, this:

 cat $args[0] | C:\PythonWrapper\plink -ssh root@192.168.1.230 -pw passw0rd 'cd testProject/testProject;python3 -' 

Where testProject is the name of the future project.

IDE Setup


Let us make the shell interpreter run instead of the Python interpreter.
Having created the project, go to the settings File-> Settings or Ctrl-Alt-S. In the project tab - Project Interpreter. Click Add Local:



Add:



IDE swears, but adds to the list. Click “More ..” and rename to wrapper:



It is better to return the Project Interpreter back to version 3.2 (I have 3.4), otherwise the hints and auto-completion will fall off.

Here everything, we will open configurations of start Run-> Edit Configurations. And add two config files:

- Common for local debugging:



- And our monster for remote launch:



Where we choose our wrapper as an interpreter and set a constant parameter - the path to the python.ps1 file. Thus, powershell will actually be invoked, which will execute the python.ps1 script passed to it with the transfer of all subsequent parameters.

Now you can write Hello World and execute it on the target machine, but in this form we can only test projects from one file. To upload the entire project to the server, add an external tool here.



Everything. Now, with the remote configuration selected, after clicking the Run button, the project will be uploaded to the remote server, where the main.py script will be run, the stdout of which will be displayed back to the pycharm console.

Improvement


For some flexibility and convenience of customization, you can add python.ps1 script generation using the project name, and transfer the remote host data to one place.

deploy.ps1
 $dhost = '192.168.1.230' $password = 'passw0rd' $login = 'root' "cat `$args[0] | C:\PythonWrapper\plink -ssh $($login)@$($dhost) -pw $($password) 'cd {0}/{0};python3 -'" -f $args[1]> .\python.ps1 $duser = $login + '@' + $dhost .\plink -ssh $duser -pw $password 'rm -rf '$args[1]';mkdir -p '$args[1] $dpath = $duser + ':~/' + $args[1] + '/' .\pscp -r -scp -pw $password $args[0] $dpath 


Now, to configure a new server, you will need to change only three lines, and when creating a new project, add the remote configuration to it.

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


All Articles