简体   繁体   English

将当前文件目录添加到 PyCharm 中的 Python 解释器路径

[英]Add current file directory to Python interpreter path in PyCharm

PyCharm allows customization of the Python console. PyCharm 允许自定义 Python 控制台。 By default it adds WORKING_DIR_AND_PYTHON_PATHS to the sys.path :默认情况下,它将WORKING_DIR_AND_PYTHON_PATHS添加到sys.path

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS,WORKING_DIR_AND_PYTHON_PATHS + '/..', FILE_DIR])

在此处输入图像描述

Is there any variable for the "current file directory" (not current working directory)? “当前文件目录”(不是当前工作目录)是否有任何变量? This is needed to make relative paths work to other modules in same directory.这是使相对路径适用于同一目录中的其他模块所必需的。

This can be done (using option 2 below) but not using the GUI shown in the question (option 1).这可以完成(使用下面的选项 2),但不能使用问题中显示的 GUI(选项 1)。 It must be noticed there are 2 very different ways to launch the Console inside PyCharm.必须注意,在 PyCharm 中启动控制台有两种截然不同的方式。

1. As shown in the question by going to File > Settings > Build, Execution, Deployment > Console > Python Console . 1.如问题所示,转到File > Settings > Build, Execution, Deployment > Console > Python Console

2. Or using the Run/Debug configuration at Run > Edit Configurations . 2.或者在Run > Edit Configurations使用 Run/Debug 配置。

What the GUI does in the 2 cases is very different GUI 在这两种情况下的作用非常不同

1. In the first case , the IDE simply calls the OS shell with the Python interpreter as first argument and the path to the Console plugin as second argument. 1.在第一种情况下,IDE 只需调用操作系统 shell,Python 作为第二个解释器作为第一个参数,并使用控制台插件的路径。

C:\path_to_venv\Scripts\python.exe

"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py"

--mode=client --port=12345

There is one single variable of IDE magic to this (see also the comments in this answer ): IDE 魔术只有一个变量(另请参阅此答案中的评论):

Console. 安慰。 Python Console Python 控制台

The WORKING_DIR_AND_PYTHON_PATHS variable is hardcoded in PyCharm. WORKING_DIR_AND_PYTHON_PATHS变量在 PyCharm 中硬编码。 It displays two paths: the project root and the working directory.它显示两个路径:项目根目录和工作目录。

This means the IDE does not expose any other "magic variable" that would allow to retrieve the file before or after the Interpreter/Console is being called.这意味着 IDE 不会公开任何其他允许在调用解释器/控制台之前或之后检索文件的“魔术变量”。 Neither Python nor the OS have any way of knowing at this point what file/module you want to use, the only way would be hardcoding the file path as an environment variable (but this doesn't solve anything, because you would have to change the path every time you change file.) Python 和操作系统此时都无法知道您要使用哪个文件/模块,唯一的方法是将文件路径硬编码为环境变量(但这并不能解决任何问题,因为您必须更改每次更改文件时的路径。)

2. The second option , does allow to transparently pass the module/file you currently have opened in the editor when you call the Console. 2.第二个选项,确实允许在调用控制台时透明地传递您当前在编辑器中打开的模块/文件。

Basically by creating a run/configuration "template" using a using the " FileDir macro" whenever you run the debugger on any module opened in the editor a "temporary configuration" is created for that module that allows to retrieve the macro value from sys.argv .基本上,每当您在编辑器中打开的任何模块上运行调试器时,通过使用“ FileDir宏”创建运行/配置“模板” ,就会为该模块创建“临时配置”,允许从sys.argv In this case the file is chosen by the IDE on-the-fly and the macro passes the path along with it.在这种情况下,文件由 IDE 即时选择,并且宏将路径与它一起传递。

C:\path_to_venv\Scripts\python.exe

"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevd.py"

--multiproc --qt-support=auto --client 127.0.0.1 --port 12345

--file C:/path_to_module/teste2.py C:\path_to_module

This 2nd option is how the Console is supposed to be used in PyCharm to get the functionality in the question, as shown in the screenshot.这第二个选项是应该如何在 PyCharm 中使用控制台来获得问题中的功能,如屏幕截图所示。

在此处输入图像描述

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

相关问题 将目录添加到 PyCharm 中的 Python 路径? - Add directory to Python path in PyCharm? 为什么 PyCharm 将当前文件和父目录添加到路径? - Why PyCharm adds current file and parent directory to path? PyCharm在Docker中添加远程Python解释器 - PyCharm add remote Python interpreter inside the Docker 在 Pycharm 添加 python 解释器屏幕中找不到 WSL 解释器 - cant find WSL interpreter in Pycharm add python interpreter screen 当我在Pycharm中创建新的python文件时,是否可以包含解释器路径(或设置任何默认代码)? - Is it possible to include interpreter path (or set any default code) when I create new python file in Pycharm? PyCharm Jupyter笔记本:将Jupyter添加到当前项目的解释器中 - PyCharm Jupyter notebook : Add Jupyter to the interpreter of the current project 尽管改变了pyCharm python解释器路径,但无法将pandas导入pycharm解释器 - Can't import pandas into pycharm interpreter, despite changing pyCharm python interpreter path 通过解释器运行文件会更改当前目录吗? - Running a file via the interpreter changes current directory? 如何将目录或制作目录添加到当前路径并使用文件 - How to add directory or make directory to current path and use file Docker远程上的Pycharm远程解释器:[Errno 2]没有此类文件或目录 - Pycharm Remote interpreter on Docker remote: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM