简体   繁体   English

pycharm ssh interpter 没有那个文件或目录

[英]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我使用 macbook pro 15 作为本地机器,并且我有一个运行 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:当我尝试使用 pycharm 运行一个简单文件时,我收到此错误:

ssh://donbeo@149.157.140.205: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.大多数问题确实指的是旧版本的pycharm。

It is clear that the file is not in my remote machine because I create it with pycharm in my local one.很明显,该文件不在我的远程机器中,因为我在本地机器中使用 pycharm 创建了它。 I was expecting pycharm to do some sort of synchronisation between the local and remote machine.我期待 pycharm 在本地和远程机器之间进行某种同步。

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.转到文件 -> 设置 -> 项目:{project_name} -> 项目解释器。
  2. Click on cog icon and select Add Remote.单击齿轮图标并选择添加远程。
  3. Add your SSH host credentials and interpreter path (on remote machine).添加您的 SSH 主机凭据和解释器路径(在远程机器上)。
  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) .因此,您应该在项目解释器下拉选择器中看到新位置,拼写为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创建新的部署设置并填写 ssh 主机配置
    • Type: SFTP类型:SFTP
    • SFTP host: same as interpreter host SFTP 主机:与解释器主机相同
    • Root path: path where files will be uploaded根路径:上传文件的路径
  3. Click on button "Test SFTP connection" to check if provided data are correct.单击“测试 SFTP 连接”按钮以检查提供的数据是否正确。
  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.部署路径相对于根路径- /相当于/my/root/path/dir相当于/my/root/path/dir等。

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.代码应该在远程机器上运行。

Be sure your interpreter is chosen as SSH which is to compile, not STFP which is for deployment.确保您的解释器被选择为SSH用于编译,而不是STFP用于部署。

在此处输入图片说明

Sometimes you can have everything set up correctly (interpreter, deployment, Path mapping, etc) but PyCharm keeps trying to use your local path on the remote machine .有时您可以正确设置所有内容(解释器、部署、路径映射等),但 PyCharm 一直尝试在远程机器上使用您的本地路径。

If this happens, try this 4 fixes:如果发生这种情况,请尝试以下 4 个修复程序:

  1. Go to File->Invalid Caches / Restart转到File->Invalid Caches / Restart

...if the problem is still not fixed: ...如果问题仍未解决:

  1. Go to Preferences->Python Interpreter->Path Mappings and check the path mappings of the interpreter.转到Preferences->Python Interpreter->Path Mappings并检查解释器的路径映射 They should look more or less like this:它们应该或多或少像这样:

SSH 路径映射

...again, if the previous step didn't work: ...同样,如果上一步不起作用:

  1. Go to Run->Edit Configurations... and check if the path mappings are correct:转到Run->Edit Configurations...并检查路径映射是否正确:

PyCharm 路径映射运行/调试

  1. You can also try to replace the local paths of the script and working directory with the remote ones (as if you were already in that machine).您还可以尝试用远程路径替换脚本工作目录的本地路径(就好像您已经在该机器中一样)。

pycharm ssh 解释器路径

  1. If you get ModuleNotFoundError but the connection seems to work, go to Run->Edit Configurations...->Environment Variables and add your project's root path to the PYTHONPATH variable.如果您收到ModuleNotFoundError但连接似乎正常,请转到Run->Edit Configurations...->Environment Variables并将您的项目的根路径添加到PYTHONPATH变量。

I'm running PyCharm 2018.3我正在运行PyCharm 2018.3

The Problem:问题:

First, I didn't have the [Errno 2] No such file or directory problem.首先,我没有[Errno 2] No such file or directory问题。 Then I had the problem out of nowhere.然后我突然遇到了这个问题。

The issue arose when I did:当我这样做时出现了问题:

  • Preferences -> Project: {project_name} -> Project Interpreter首选项 -> 项目:{project_name} -> 项目解释器
  • Instead of "Click on cog icon and select Add Remote", I chose a interpreter that I've configured previously (that is working fine previously) from the drop down menu我没有选择“单击齿轮图标并选择添加远程”,而是从下拉菜单中选择了我之前配置的解释器(之前工作正常)

By doing the steps above, I had [Errno 2] No such file or directory problem.通过执行上述步骤,我遇到了[Errno 2] No such file or directory问题。

The reason is that the folder is not automatically synced to the remote server (a bug?), nor could I find a way to configure it to sync.原因是该文件夹不会自动同步到远程服务器(错误?),我也找不到将其配置为同步的方法。 Configuring Deployment and Path Mappings did work, but it's too much manual labor and prone to error.配置DeploymentPath Mappings确实有效,但人工劳动太多,容易出错。

The solution:解决方案:

All I had to do is to add a new remote interpreter, despite that identical configurations already exists previously.我所要做的就是添加一个新的远程解释器,尽管之前已经存在相同的配置。 (To be safe, I did remove the old ssh interpreter and the old deployments, also removed .idea folder) Then everything worked as expected. (为了安全起见,我确实删除了旧的 ssh 解释器和旧部署,还删除了 .idea 文件夹)然后一切都按预期进行。

  • I didn't need to configure Deployment我不需要配置部署
  • I didn't need to configure Paths mappings我不需要配置路径映射

It can be caused by one of the following:它可能是由以下原因之一引起的:

1. You did not deploy your code on the remote server.! 1. 你没有在远程服务器上部署你的代码。!

  • Make sure that the recent edition of your code is deployed on the server.确保您的代码的最新版本部署在服务器上。 You can always deploy your code from Tools -> Deployment -> Upload to ..., or by using Ctrl + Shift + Alt + x.您始终可以从工具 -> 部署 -> 上传到 ... 或使用 Ctrl + Shift + Alt + x 部署代码。
  • For setting the deployment path, Go to File -> Settings -> Build, Execution, Deployment -> Deployment, and enter a remote sever details as your deploying machine.要设置部署路径,请转到文件 -> 设置 -> 构建、执行、部署 -> 部署,然后输入远程服务器详细信息作为您的部署机器。
  • In the Mappings tab, make sure that the deployment path is defined somewhere on the server where you have permission to read and edit files there.在“映射”选项卡中,确保在服务器上您有权读取和编辑文件的某个位置定义了部署路径。

2. You do not have the permission to read and change files on your deployment path at the remote server. 2. 您无权在远程服务器上读取和更改部署路径上的文件。

  • Go back to File -> Settings -> Build, Execution, Deployment -> Deployment, and change deployment path on the Mappings tab.返回 File -> Settings -> Build, Execution, Deployment -> Deployment,并在 Mappings 选项卡上更改部署路径。

对于那些遇到“miniconda3/envs/gpu/bin/python: can't open file '///home/.../test.py”这样的问题的人,你可以在pycharm的locality中运行python脚本文件而不是在 pycharm 的服务器中运行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM