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

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

问题描述

我正在使用通过ssh访问的远程Python解释器(使用pew创建的虚拟环境).手动向服务器ssh'并使用解释器时,此方法工作正常.但是,当使用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

但是仅向PyCharm提供此Shell文件作为解释器是行不通的(软件包管理已禁用,并且在运行py代码时会打开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远程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天全站免登陆