PyDev: Running Code in local machine to remote machine - python

Would you please let me know, how would I run my code in local machine to remote server?
I have source code and data in local machine. But I would like to run the code in remote server.

One solution would be:
Install python on the remote machine
Package your code into a python package using distutils (see http://wiki.python.org/moin/Distutils/Tutorial). Basically the process ends when you run the command python setup sdist in the root dir of your project, and get a tar.gz file in the dist/ subfolder.
Copy your package to the remote server using scp, for example, if it is an amazon machine:
scp -i myPemFile.pem local-python-package.tar.gz remote_user_name#remote_ip:remote_folder
Run sudo pip install local-python-package.tar.gz on the remote server
Now you can either SSH to the remote machine and run your code or use some remote enabler such as fabric to start commands on the remote server (works for any shell command, specifically python scripts)
Alternatively, you can just skip the package building in [2], if you have a simple script, just scp the script itself to the remote machine ane proceed using a remote python myscript.py
Hope this helps

I would recommend setting up git repository on repote server and connect local source (for git you can read about how to do it here: http://git-scm.com/book).
Then you can use i.e. Eclipse EGit and after you change your local code you can push it to remote location.

Related

How to activate venv- remote interpreter- VM from GCP connected via SSH

I would like to activate the venv. I'am using the remote interpreter because pycharm has got the connection via SSH with GCP VM. I used to activate env by using this command:
On Unix or MacOS, using the bash shell: source /path/to/venv/bin/activate
In local mode there is no tackle with it but for remote interpreter I do not know how to find the source. Could you please help me with this tackle?

Mounting a virtual environment via SSHFS on local machine using it's Python3 file not working

So I have mounted a part of a development server which hold a virtual environment that is used for development testing. The reason for this is to get access to the installed packages such as Django-rest-framework and Django itself and not having it set up locally (to be sure to use the same version as the development server has). I know that it's perhaps better to use Docker for this, but that's not the case right now.
The way I've done it is installing SSHFS via an external brew (as it's no longer supported in the brew core) - via this link https://github.com/gromgit/homebrew-fuse
After that I've run this command in the terminal to via SSH mount the specific part of the development server that holds the virtual enviornment:
sshfs -o ssh_command='ssh -i /Users/myusername/.ssh/id_rsa' myusername#servername:/home/myusername/projectname/env/bin ~/mnt/projectname
It works fine and I have it mounted on my local disk in mnt/projectname.
Now I go into VSCode and go into the folder and select the file called "python3" as my interpreter (which I should, right?). However, this file is just an alias, being 16 bytes in size. I suspect something is wrong here, but I'm not sure on how to fix it. Can someone maybe take a look and give some input? I'll attach a screenshot of the mounted directory.
Screenshot of virtualenv directory mounted on local machine
The solution to the problem was using the VSCode extension Remote - SSH and run VSCode directly in the remote location, and from there being able to access the virtual environment.

How to run a python script on a remote ec2 instance using sudo from pycharm

I am running pycharm 2017.2.3. I want to run my python script on a remote ec2 instance using sudo user through pycharm. How do I acieve this?
Follow the steps below:
Go to File -> Settings -> Project Interpreter and add a new interpreter
Click on + to add a new python interpreter and then click on SSH interpreter
Provide your EC2 Public DNS in HOST and ubuntu as username
Click Next and add the private_key.pem file.
See this article for more details:
PyCharm setup for AWS automatic deployment
It looks like you can configure your python interpreter over SSH with the professional version of PyCharm.
Configuring Remote Interpreter + PyCharm
Finally found an answer after a researching through the internet. We can have a script on remote machine as a pycharm interpreter. Create a following script on a remote machine and make sure the script is executable.
#!/bin/bash
sudo /usr/bin/python "$#"
Now change the project interpreter to point to the above script on remote machine in pycharm. Now every script you run on local machine gets executed on remote as a sudo user.

How do I run a subprocess using local librairies on a remote server using mounted file systems?

I am using a subprocess to run a program on another machine through a mounted network file system.
For my first step in this project I have been using sshfs mount and wget:
sys_process = subprocess.Popen(wget_exec.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
using the command: wget works perfectly
using the command: /mnt/ssh_mount/wget does not execute
System libraries:
my remote system is: Arch Linux which is calling for libpcre.so.1
my local system is: Ubuntu which uses libpcre3 so libpcre.so.1 is missing
I know this because when I call the wget command through the ssh mount (/mnt/ssh_mount/bin/wget) it throws an error. I do not wish to install needed libraries to all systems using this as it defeats the purpose of trying to run something remotely.
For measure checks for permissions have been made
How do I get the command to use the local libraries?
I hope to use nfs as well which would exclude below as solutions:
Python subprocess - run multiple shell commands over SSH
Paramiko
I have tried (with no success)
os.chdir('/mnt/ssh_mount')
Error while loading shared libraries: 'libpcre.so.0: cannot open shared object file: No such file or directory' assumes a stable mount point which would cause changes in 2 places when the environment would change (this seems wrong from a database normalization background, I would assume code/sys admin as well)
You are not actually running the wget command on the remote machine - you are trying to run the remote machine's binary on your local system, and the command is failing due to incompatible library versions. sshfs, nfs, and other types of network mounting protocols simply mount the remote filesystem as an extension of your local one - they don't allow for remote execution. To do that, you'll need to open a remote shell using ssh tunneling and execute the Arch wget command through that.

virtual env and python client server in linux

I have a simple django python server process which needs to be executed in linux environment (in a virtualenv python environment)
Currently one of my colleague manually logs into ssh console and starts the virtual environment via source bin/activate command. Thereafter python server is started using below command
/etc/init.d/start-python-server.sh
Note: This sh file starts the python server as a background process listening in port 8080
Can some one give some thoughts on improving this?
Please share in your thoughts.
you may include source <your_env_path>/bin/activate at the beginning of /etc/init.d/start-python-server.sh to automate this process

Categories