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

编程入门 行业动态 更新时间:2024-10-27 16:33:26
本文介绍了如何使用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解释器保留环境变量

本文发布于:2023-06-05 01:13:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   环境变量   PyCharm   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!