在PyCharm中,单击Markdown文件中的命令的控制台等效操作是什么?

huangapple go评论102阅读模式
英文:

What is the console equivalent of clicking on a command in a markdown file in PyCharm?

问题

我有一个包含README.mdrun.py文件夹,内容如下:

import os

assert __name__ == '__main__'

print('■', __file__)
print('●', os.getcwd())

try:
    os.mkdir('DELETE_ME')
except FileExistsError:
    pass

readme文件包含以下代码行:python -m a001_misc.b006_cwd.folder.run
PyCharm显示它旁边有一个绿色三角形。
当我点击它时,输出告诉我,文件夹是我的CWD。

这是期望的行为。(最重要的是,DELETE_ME被创建在文件夹中。)
但是我找不到一个一行的控制台命令来复制这个(即不使用cd)。

我想知道,当我点击它时实际发生了什么。
我找到的最接近的等效命令是在文件夹中运行python -m run
(在文件夹中运行整个命令会引发ModuleNotFoundError。)

readme还包含以下代码行:python run.py
通常不会引起问题。点击它与在文件夹中运行命令的效果相同。
但是有一个小bug,也许它可以帮助回答这个问题。
我已将文件夹的父目录从b006_mswitch_confusion重命名为b006_cwd
但不知何故,旧名称仍与此readme中的按钮相关联。

旧名称在哪里仍然隐藏着?
(我已删除了文件夹中的__pycache__。)

示例代码也可以在这里找到。
(readme文件包含运行脚本的不同方式的输出。)

英文:

I have a folder with a README.md and a run.py that looks like this:

import os


assert __name__ == '__main__'

print('■', __file__)
print('●', os.getcwd())


try:
    os.mkdir('DELETE_ME')
except FileExistsError:
    pass

The readme contains the code line python -m a001_misc.b006_cwd.folder.run.<br>
PyCharm shows a green triangle next to it.<br>
When I click on it, the output tells me, that folder is my CWD.

在PyCharm中,单击Markdown文件中的命令的控制台等效操作是什么?

This is the desired behavior. (Above all, DELETE_ME is created in folder.)<br>
But I do not find a one-line console command to reproduce this (i.e. without cd).

I would like to know, what actually happens, when I do that click.<br>
The closest equivalent I have found is to do python -m run in folder.<br>
(While running the whole command in folder creates a ModuleNotFoundError.)

The readme also contains the code line python run.py.<br>
Normally it raises no questions. Clicking it does the same as running the command in folder.<br>
But there is a small bug, and maybe it can help to answer the question.<br>
I have renamed the parent of folder from b006_mswitch_confusion to b006_cwd.<br>
But somehow the old name is still connected with this button in the readme.<br>

在PyCharm中,单击Markdown文件中的命令的控制台等效操作是什么?

Where is that old name still hidden?<br>
(I have already deleted the __pycache__ in folder.)

The example code can also be found here.<br>
(The readme file contains the outputs for different ways to run the script.)

答案1

得分: 2

在bash中的控制台等效操作是
```bash
(cd 文件夹; conda 激活环境或激活虚拟环境; 路径/到/python -m my_script)

my_script 应该通过 PYTHONPATH 找到,所以要使 python -m a001_misc.b006_cwd.folder.run 起作用,examples_py 应该在你的 PYTHONPATH 中。

由于解释器总是首先搜索当前目录,你可以将其替换为 python -m run 以实现可移植性。


<details>
<summary>英文:</summary>

a console equivalent in bash would be 
```bash
(cd folder; conda activate env or activate venv; path/to/python -m my_script)

my_script should be found using the PYTHONPATH, so for python -m a001_misc.b006_cwd.folder.run to work, examples_py should be on your PYTHONPATH.

since the current directory is always searched first by the interpreter you could just replace it with python -m run for portability.

答案2

得分: 0

为了回答第一个问题:在你的截图中,第一行(以蓝色显示)是PyCharm执行的命令。在你的情况下,它会将python替换为虚拟环境中特定的Python可执行文件。

为了回答第二个问题:可能是PyCharm本身的缓存过期了,与__pycache__无关。相反,通过转到文件 > 使缓存失效 (invalidate the caches in the IDE) 来使IDE的缓存失效。

英文:

To answer the first question: In your screenshot, the very first line (printed in blue) is the command that PyCharm executes. In your case, it replaces python with a particular Python executable from the virtual environment.

To answer the second question: It can be that the caches of the PyCharm itself are stale, and they have nothing to do with __pycache__. Instead, invalidate the caches in the IDE by going to File > Invalidate Caches

答案3

得分: 0

答案和Ahmed AEK的评论包含所有重要的事实。无论如何,我认为对我的问题的答案可以简化为以下内容:

点击命令将cd到Markdown文件所在的文件夹并运行命令。

但这发生在一个环境中,PyCharm已将项目文件夹添加到PYTHONPATH中。

可以说,在控制台中没有精确的等效点击,因为从控制台可能无法确定PyCharm认为的项目文件夹是什么。

英文:

The answer and comments by Ahmed AEK contain all the important facts.<br>
Anyway, I think the answer to my question can be simplified to the following:

Clicking on the command will cd into the folder of the markdown file and run the command.

But this happens in an environment, where PyCharm has added the project folder to PYTHONPATH.

One could say, that there is no exact console equivalent of the click,
because there is probably no way to figure out from the console, what PyCharm considers to be the project folder.

huangapple
  • 本文由 发表于 2023年8月10日 21:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定