简体   繁体   English

如何使用 PyCharm 为远程 Python 解释器保留环境变量

[英]How to keep environment variables for remote Python interpreter with PyCharm

I am using a remote Python interpreter (a virtual environment created using pew) that I access via ssh.我正在使用通过 ssh 访问的远程 Python 解释器(使用 pew 创建的虚拟环境)。 This is working fine when manually ssh'ing to the server and using the interpreter.当手动 ssh 连接到服务器并使用解释器时,这工作正常。 However when using PyCharm to call it, it forgets about the environment variables that I export in my .bashrc.但是,当使用 PyCharm 调用它时,它会忘记我在 .bashrc 中导出的环境变量。

It seems when calling the Python Interpreter via ssh, PyCharm (even though started from bash) ignores the .bashrc.似乎在通过 ssh 调用 Python 解释器时,PyCharm(即使从 bash 开始)忽略了 .bashrc。 Here it is indicated that a shell wrapper is the solution. 这里表明外壳包装器是解决方案。

#!/bin/bash -l
/path/to/interpreter/bin/python

But simply giving PyCharm this shell file as an Interpreter doesn't work (package management is disabled and when running py-code a Python terminal opens...).但是简单地将这个 shell 文件作为解释器提供给 PyCharm 是行不通的(包管理被禁用,并且在运行 py-code 时会打开一个 Python 终端......)。

How do I get a "shell wrapper" that makes sure .bashrc is executed before the PyCharm remote Python-interpreter is run?如何获得“shell 包装器”以确保在运行 PyCharm 远程 Python 解释器之前执行 .bashrc?


I made sure that PyCharm runs with the correct environment.我确保 PyCharm 在正确的环境下运行。 All local interpreters don't show that problem (testing in PyCharm terminal and with a Python script:)所有本地解释器都没有显示该问题(在 PyCharm 终端和 Python 脚本中进行测试:)

import os
print(os.environ)

I am also aware of the "Edit Configuration" option for running specific files and the possibility to set environment variables there, however it is tedious to that for each project/file and not what I'm looking for.我也知道用于运行特定文件的“编辑配置”选项以及在那里设置环境变量的可能性,但是对于每个项目/文件而不是我正在寻找的内容来说,这很乏味。

You want your wrapper to contain the "$@" when calling python in your wrapper:在您的包装器中调用 python 时,您希望您的包装器包含“$@”:

#!/bin/bash -l
/path/to/interpreter/bin/python "$@"

With that trick I somehow managed to setup pycharm on Windows while using virtualenv in WSL (Ubuntu bash) and retrieving my WSL env variables.通过这个技巧,我以某种方式设法在 Windows 上设置 pycharm,同时在 WSL(Ubuntu bash)中使用 virtualenv 并检索我的 WSL env 变量。

I have also been using the solution from the link you provided in the question, but with two modifications:我也一直在使用您在问题中提供的链接中的解决方案,但有两个修改:

1. .profile instead of .bashrc 1. .profile而不是.bashrc

My .bashrc contains commands that make working in an interactive shell easier (setting aliases, activating a virtual environment).我的.bashrc包含使在交互式 shell 中工作更容易的命令(设置别名、激活虚拟环境)。 In .profile , I have commands that both interactive and non-interactive shells need, for example setting paths to important libraries..profile ,我有交互式和非交互式 shell 都需要的命令,例如设置重要库的路径。

I source .profile instead of .bashrc for the remote Python interpreter to avoid cluttering the environment unnecessarily and to ensure the same setup as calling python in a non-interactive (login) shell.我为远程 Python 解释器提供.profile而不是.bashrc以避免不必要地混乱环境并确保与在非交互式(登录)shell 中调用python相同的设置。

Furthermore, the shell wrapper that you specify as a remote interpreter must be silent .此外,您指定为远程解释器的 shell 包装器必须是 silent If you source .bashrc and it contains a command that prints a message like "Activated my_conda_env" , this will mess with PyCharm's remote debugger.如果您获取.bashrc并且它包含一个打印消息的命令,例如"Activated my_conda_env" ,这将与 PyCharm 的远程调试器"Activated my_conda_env"

2. Replacing python3.x with the shell wrapper 2.用shell包装器替换python3.x

PyCharm's remote interpreter and remote debugger don't require the shell wrapper to have a specific name. PyCharm 的远程解释器和远程调试器不需要外壳包装器具有特定名称。 However, I have observed that other functionality, like PyCharm's package tools (pip etc.), expect the interpreter to have a standard name ( python , python3 or python3.6 ).但是,我观察到其他功能,如 PyCharm 的包工具(pip 等),期望解释器具有标准名称( pythonpython3python3.6 )。 Thus, I needed to replace the python3.6 command with the shell wrapper itself.因此,我需要用 shell 包装器本身替换python3.6命令。

This has the benefit that logging in to the remote server and calling python there will also execute the wrapper.这样做的好处是登录到远程服务器并在那里调用python也将执行包装器。 Thus, the behavior is the same as calling it through PyCharm on a local machine.因此,该行为与在本地机器上通过 PyCharm 调用它的行为相同。

Solution解决方案

The final solution looks like this:最终的解决方案如下所示:

  1. In the bin directory of the virtual environment / conda environment:在虚拟环境/conda环境的bin目录下:

     $ mv python3.6 python3.6_original $ touch python3.6 $ chmod +x python3.6
  2. In python3.6 :python3.6

     #!/bin/bash # Make sure that the profile was sourced, so that executing python # via ssh does the same as executing python locally. if ! (( $PROFILE_SOURCED )); then . ~/.profile fi # Get the directory of this script, see # https://stackoverflow.com/a/53122736/2305095 __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Execute the original python. ${__dir}/python3.6_original "$@"
  3. At the end of .profile :.profile的末尾:

     export PROFILE_SOURCED=1

python and python3 are symlinks to python3.6 by default, so they automatically point to the shell wrapper.默认情况下, pythonpython3python3.6的符号链接,因此它们会自动指向 shell 包装器。

If you are using conda, you should also check in python3.6 whether .bashrc was already sourced.如果您使用的是 conda,您还应该在python3.6检查.bashrc是否已经被提供。 conda updates the PATH in .bashrc (and advises you to keep it there). conda 更新.bashrc的 PATH(并建议您将其保留在那里)。 For the remote Python interpreter, you might want to also update the PATH in the same way in python3.6 and include the paths to relevant bin directories (for example the global condabin and the respective environment's bin directory).对于远程 Python 解释器,您可能还希望在python3.6中以相同的方式更新 PATH 并包含相关bin目录的路径(例如全局condabin和相应环境的bin目录)。

I have been using this solution for 6 months now and have been happy with it so far.我已经使用这个解决方案 6 个月了,到目前为止一直很满意。 Let me know if you encounter any problems!如果您遇到任何问题,请告诉我!

try this, it may work.试试这个,它可能会起作用。 top right-hand corner->edit configurations->environment variable->add your variable右上角->编辑配置->环境变量->添加变量

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

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