简体   繁体   English

PyCharm 远程调试使用远程容器 Python 解释器

[英]PyCharm remote debug using remote container Python interpreter

My situation is that I have set up a container in a remote server, and inside the container, there is a virtual environment.我的情况是我在远程服务器上搭建了一个容器,容器内部是一个虚拟环境。 I'm using the python interpreter inside this virtual environment in this container, not the one on the host.我在这个容器的这个虚拟环境中使用 python 解释器,而不是主机上的解释器。

From my local machine, I can open up PyCharm, and use Tools->Deployment->Configuration to easily set up a remote connection.在我的本地机器上,我可以打开 PyCharm,然后使用Tools->Deployment->Configuration轻松设置远程连接。 And For a specific project, I can set up the interpreter by clicking Files->Settings->Project Interpreter .对于特定项目,我可以通过单击Files->Settings->Project Interpreter来设置解释器。 However, it seems that I can only select the host Python interpreter( /usr/bin/python ) on the remote server, not the one inside the virtual environment in the container.但是,似乎我只能在远程服务器上 select 主机 Python 解释器( /usr/bin/python ),而不是容器中虚拟环境中的那个。 How could I set up using this interpreter?我如何设置使用这个解释器?

I googled but can't find exact solution.我用谷歌搜索但找不到确切的解决方案。 I don't think I need to install Docker locally because my Docker is on the remote server side, right?我认为我不需要在本地安装 Docker 因为我的 Docker 在远程服务器端,对吧?

In similar way you are connecting to remote host - you would need to setup container with same capabilities eg set ssh server running on there.以类似的方式连接到远程主机 - 您需要设置具有相同功能的容器,例如设置 ssh 服务器在那里运行。 Then you should expose the port into public world or use nested ssh tunnel, which would be better alternative.然后您应该将端口公开到公共世界或使用嵌套的 ssh隧道,这将是更好的选择。

Another interesting approach (maybe recommended) is to forward Docker socket from the remote machine so, that you local Docker CLI uses this socket for sending commands to remote host.另一种有趣的方法(可能推荐)是从远程机器转发 Docker 套接字,以便本地 Docker CLI 使用此套接字向远程主机发送命令。 Theoretically, then you could add this container directly in PyCharm, when you set correct Docker host address there.理论上,当您在那里设置正确的 Docker 主机地址时,您可以直接在 PyCharm 中添加此容器。

Further, virtual environments on other than local host systems are not supported natively by PyCharm.此外,PyCharm 本身不支持本地主机系统以外的虚拟环境。 However, you could try to add path of python and see if it works eg venv/bin/python from project directory.但是,您可以尝试添加python的路径并查看它是否有效,例如项目目录中的venv/bin/python

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

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