Opening script saved on a cluster with local spyder installation - python

I have anaconda, hence spyder, installed on a local machine. What I am trying to do is to use my local spyder installation to open a .py script saved on a remote cluster (in my office) via ssh. The issues that I am encountering are the following:
I cannot run spyder from the cluster - there is no graphical device whatsoever. For example, we have actually anaconda installed on the cluster, but when I ran spyder from the command line, I get the following error message: Could not connect to any X display
I cannot mount the (remote) drivers, where the .py scripts are located, onto my local machine when I am working from home (which is the case when I am at work, connected to the internet via cable). If this was the case, I could simply launch spyder on my local machine, then open the scripts. I can only access the files on some drivers mounted onto the cluster via ssh.
As, however, I can access the .py scripts saved on the cluster via ssh (I can open then with programs installed locally e.g. vim, jpico etc), I was wondering whether it is possible to use the command line to open a script saved on a remote cluster using my local spyder installation, something like $ spyder /path/to/myScript/savedOnTheRemoteCluster.py

(Spyder maintainer here) As of May 2019 our editor is not capable of working with files on remote locations. So your best option right now is to mount your remote server with sshfs to make it appear as a local directory and then open any file present there in Spyder.

Related

Remote VSCode notebook looks for python on local machine path

I am having trouble with my vscode and ssh connection.
I have installed miniconda on my local machine at home/sam/miniconda3, and I have installed anaconda on a remote HPC machine on home/sam/anaconda3.
I have replicated conda envirenments in both using a yml file. Recently, the I keep getting errors while connecting to ssh that "python was not found". After digging for a while, I realize that when running a jupter nb, it looks for python under the local machine path (home/sam/miniconda3). Despite the case that I have selected the kernel from the remote machine path (/home/sam/anaconda3/envs/myenv/bin/python3.9).
How can I get the notebook in vscode to look at the remote path when connected to the remote machine?
Thanks heaps!

Cannot find python kernels corresponding to Virtual environment on remote

I am visual studio code Version: 1.63.2 (Universal).
I am connected to a linux server where my code is located (jupyter notebook). When i open the notebook and select the python kernel, i can only find the default python kernels (from the server). I cannot find the kernels that i created which is necessary to run the notebooks.
Note: The required kernel is linked to a virtual environment on the kernel.
Please help me in linking the remote kernel to my notebook on vscode so that i can work locally on the notebooks located in remote server.
I needed to install the python extension on the remote server.
You can find the answer in the second method here.
https://stackoverflow.com/a/62871909
I did a little differently:
Ctrl+Shift+P, Preferences: Open Remote Setting (SSH:your_address)
Set Python: Default Interpreter Path to your virtual environment Python path
Done
i've stumbled upon this post, since i had a similar issue.
working remotely on a linux server, even if i selected the right interpreter (via shift+ctrl+P "Select Interpreter to start Jupyter server") the kernel remained unactive.
i've checked the installed dependencies inside the venv and tried to switch virtual environment to make it work.. kept on reloading the server, reloading the window.. no way.
eventually, a tiny fancy detail arouse my attention: the "Jupyter server : remote" label in the bottom right.
and tadaa : that was my issue. I've selected "default", letting VSCode starting a server on the local (remote) host, and then the interpeter / kernel was enabled.
hope it can help anyone stuck on the same issue. jupyter server location detail

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.

Environmen PATH with Pycharm remote connection

I have a remote server and I want to use Pycharm to execute my scripts on this remote server using SFTP and SSH. Everything works properly, except that, when running the scripts on the server through the Pycharm "Run" command, the PATH is wrong. I am trying to understand which file I should edit to properly set the PATH environmental variable. I am using a mac and the server runs ubuntu. After having installed several libraries on the server, I edited the .bashrc file in my home directory on the server to properly set up the PATH. Then the following happens:
If I login to the server using ssh and the mac terminal, the PATH is loaded correctly.
If I open a ssh session inside pycharm and connect to the server, the PATH is loaded correctly.
If I run a simple script (that only contains calls to the standard library) on the remote server through the Pycharm "Run" command, it works
If I run the full script on the remote server through the pycharm "Run" command, then in crashes: os.environ['PATH'] shows that the path is not correct
If I run the script by directly calling the python interpreter on the server using one of the terminal ssh session, then the PATH is set up properly and it works
So apparently the PATH is set up properly if I connect to it using ssh and the terminal, but when Pycharm tries to run it directly the PATH is not updated and it crashes.
So I am guessing pycharm doesn't look into .bashrc before running the program. Which file should I modify then? Note that the only "configuration" files present in my home directory on the remote server are .bashrc and .profile.
Note I do not have sudo privileges on this server.

Opening remote project in PyCharm

I'm working on a project that is located in a remote server. Can I open it in PyCharm from my local machine ? I couldn't find the way.
The way I access the projects on my raspberry pi using PyCharm is the following (This expects you to be using PyCharm Pro, supporting SSH connections):
Mount the home folder on my local machine ( I use SFTP Drive on windows, under Linux use sshfs).
Open the project from the mounted drive in PyCharm
Go to Settings -> Project -> Project Interpreter and select the gear next to the Project Interpreter drop down. From there pick "Add Remote".
Configure the remote interpreter you want to use.
If the PyCharm project was already created on the server, I guess your run configuration should be in order and running it should work out of the box.
If you created the PyCharm project on your local machine:
If you have no run configuration yet, go to the file you want to run and do right-click -> Run yourfilename.py or simply hit Shift-F10
The remote interpreter will complain about non existent files. Go to the dropdown in the top right corner, click it and select 'Edit Configurations'
Change the script path and working directory to the actual remote directories. Remember, this is what your interpreter sees, and your interpreter is on your remote machine.
Hope this helps!
The following will configure PyCharm Professional 2021.3.1 on Windows 10 to run a project on a remote Linux server. It's likely the configuration will be similar for PyCharm on macOS.
Open PyCharm
Create a New Project and select Pure Python
Fill the Location and Remote project location, which may be new or existing.
Select the ellipsis next to Interpreter under Previously configured interpreter
Select SSH Interpreter and create New server connection
Supply your credentials
The default Interpreter will be /usr/bin/python, but an alternate location may be provided, for example /home/.../anaconda3/bin/python3.9
Create the project
Press Ctrl + Alt + S to open Settings
Select Deployment under Build, Execution, Deployment
Check Connection and Test Connection
Check Mappings to verify Local path and Deployment path
Select Python Interpreter under Project:...
Verify the remote interpreter is selected for the project.
As long as this is the case, the code will run relative to the remote system.
Close Settings with OK, or Apply
Use PUTTY or some other app to verify the project now exists on the remote system
Right click the project and select Deployment for deployment options
Note
This will likely require being connected to the corporate network through a VPN.
To update on excellent #Skusku answer given here:
https://stackoverflow.com/a/47614111/2119941
After mounting remote folder with SFTP Drive if Pycharm project browser doesn't recognize remote drive just copy-paste it in location bar on top and allow Pycharm to load it:
And when you load your project files to Pycharm you need to add remote interpreter which is present on your server:
In following screens just add host, credentials and than path to python in virtual env or whatever python is running your server project files.
Pycharm needs access to the project's directory.
You can open a remote project if the storage partition for that project directory is shared and mounted/mapped on your local machine with the right permissions by running pycharm on your machine and opening that locally visible project directory.
Note: you should not be opening the project in multiple such pycharm sessions simultaneously as they will collide/conflict with each-other.
Alternatively if you use a version control system (VCS) that supports remote repository access you can create a local copy of the project, work in that copy and push your changes to the remote project as needed (depending on your VCS specifics).
Search remote sync under plugins in pycharm. Many work. I think most common one is source sync.

Categories