如何使用 PyCharm 为远程 Python 解释器保留环境变量 [英] How to keep environment variables for remote Python interpreter with PyCharm

查看:42
本文介绍了如何使用 PyCharm 为远程 Python 解释器保留环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过 ssh 访问的远程 Python 解释器(使用 pew 创建的虚拟环境).当手动 ssh'ing 到服务器并使用解释器时,这工作正常.但是,当使用 PyCharm 调用它时,它会忘记我在 .bashrc 中导出的环境变量.

I am using a remote Python interpreter (a virtual environment created using pew) that I access via ssh. This is working fine when manually ssh'ing to the server and using the interpreter. However when using PyCharm to call it, it forgets about the environment variables that I export in my .bashrc.

似乎在通过 ssh 调用 Python 解释器时,PyCharm(即使从 bash 启动)忽略了 .bashrc.这里 表明外壳包装是解决方案.

It seems when calling the Python Interpreter via ssh, PyCharm (even though started from bash) ignores the .bashrc. Here it is indicated that a shell wrapper is the solution.

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

但是仅仅将这个 shell 文件作为解释器提供给 PyCharm 是行不通的(包管理被禁用,并且在运行 py-code 时会打开一个 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 wrapper",确保在 PyCharm 远程 Python 解释器运行之前执行 .bashrc?

我确保 PyCharm 在正确的环境下运行.所有本地解释器都没有显示该问题(在 PyCharm 终端和 Python 脚本中进行测试:)

I made sure that PyCharm runs with the correct environment. All local interpreters don't show that problem (testing in PyCharm terminal and with a Python script:)

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.

推荐答案

你希望你的包装器在你的包装器中调用 python 时包含$@":

You want your wrapper to contain the "$@" when calling python in your wrapper:

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

通过这个技巧,我设法在 Windows 上设置了 pycharm,同时在 WSL (Ubuntu bash) 中使用 virtualenv 并检索我的 WSL env 变量.

With that trick I somehow managed to setup pycharm on Windows while using virtualenv in WSL (Ubuntu bash) and retrieving my WSL env variables.

这篇关于如何使用 PyCharm 为远程 Python 解释器保留环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆