Search code examples
pythonsshpycharm

pycharm ssh interpter No such file or directory


I am using a macbook pro 15 as local machine and I have a remote server running ubuntu 14.04

I want to use the remote intepreter to run all the computation but I want to write the code from my local machine.

When I try to run a simple file with pycharm I receive this error:

ssh://[email protected]:22/usr/bin/python3 -u /Users/donbeo/Documents/phd_code/prova.py
bash: line 0: cd: /Users/donbeo/Documents/phd_code: No such file or directory
/usr/bin/python3: can't open file '/Users/donbeo/Documents/phd_code/prova.py': [Errno 2] No such file or directory

Process finished with exit code 2

I saw few people reporting the same problem but I haven't found a good answer so far. Most of the questions are indeed referring to older versions of pycharm.

It is clear that the file is not in my remote machine because I create it with pycharm in my local one. I was expecting pycharm to do some sort of synchronisation between the local and remote machine.


Solution

  • To execute your code on remote machine you'll have to perform few steps

    Define a remote interpreter for your project

    1. Go to File -> Settings -> Project: {project_name} -> Project Interpreter.
    2. Click on cog icon and select Add Remote.
    3. Add your SSH host credentials and interpreter path (on remote machine).
    4. As a result, you should see new position in project interpreter dropdown selector, spelled like Python Version (ssh://login@host:port/path/to/interpreter). Package list should be populated with records.

    Define deployment settings

    1. Go to File -> Settings -> Build, Execution, Deployment -> Deployment
    2. Create new deployment settings and fill ssh host configuration
      • Type: SFTP
      • SFTP host: same as interpreter host
      • Root path: path where files will be uploaded
    3. Click on button "Test SFTP connection" to check if provided data are correct.
    4. Go to mappings and configure mapping between local path and deployment path. Deployment path is relative to root path - / is equivalent to /my/root/path, /dir to /my/root/path/dir etc.

    Deploy your code

    1. Select Tools -> Deployment -> Upload to {deployment settings name}
    2. Upload process will be started in background. Wait for upload to complete.

    Run your code

    1. Right click on file you want to run and select "Run". Code should run on remote machine.