How to keep environment variables for remote Python interpreter with PyCharm(如何使用 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)
我也知道用于运行特定文件的编辑配置"选项以及在那里设置环境变量的可能性,但是对于每个项目/文件来说这很乏味,而不是我正在寻找的.p>
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 解释器保留环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PyCharm 为远程 Python 解释器保留环境变量


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01