英文:
What is the console equivalent of clicking on a command in a markdown file in PyCharm?
问题 {#heading}
我有一个包含README.md
和run.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.
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>
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 {#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)
</code></pre>
<p><code>my_script</code> should be found using the <code>PYTHONPATH</code>, so for <code>python -m a001_misc.b006_cwd.folder.run</code> to work, <code>examples_py</code> should be on your <code>PYTHONPATH</code>.</p>
<p>since the current directory is always searched first by the interpreter you could just replace it with <code>python -m run</code> for portability.</p>
<h1 id="2">答案2</h1>
<p><strong>得分</strong>: 0</p>
<p>为了回答第一个问题:在你的截图中,第一行(以蓝色显示)是PyCharm执行的命令。在你的情况下,它会将<code>python</code>替换为虚拟环境中特定的Python可执行文件。</p>
<p>为了回答第二个问题:可能是PyCharm本身的缓存过期了,与<code>__pycache__</code>无关。相反,通过转到<strong>文件</strong> > <strong>使缓存失效</strong> <a href="https://www.jetbrains.com/help/pycharm/invalidate-caches.html" rel="external nofollow" target="_blank">(invalidate the caches in the IDE)</a> 来使IDE的缓存失效。</p>
<details>
<summary>英文:</summary>
<p>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 <code>python</code> with a particular Python executable from the virtual environment.</p>
<p>To answer the second question: It can be that the caches of the PyCharm itself are stale, and they have nothing to do with <code>__pycache__</code>. Instead, <a href="https://www.jetbrains.com/help/pycharm/invalidate-caches.html" rel="external nofollow" target="_blank">invalidate the caches in the IDE</a> by going to <strong>File</strong> > <strong>Invalidate Caches</strong></p>
</details>
<h1 id="3">答案3</h1>
<p><strong>得分</strong>: 0</p>
<p>答案和Ahmed AEK的评论包含所有重要的事实。无论如何,我认为对我的问题的答案可以简化为以下内容:</p>
<p>点击命令将<code>cd</code>到Markdown文件所在的文件夹并运行命令。</p>
<p>但这发生在一个环境中,PyCharm已将项目文件夹添加到<code>PYTHONPATH</code>中。</p>
<p>可以说,<strong>在控制台中没有精确的等效点击</strong>,因为从控制台可能无法确定PyCharm认为的项目文件夹是什么。</p>
<details>
<summary>英文:</summary>
<p>The answer and comments by Ahmed AEK contain all the important facts.<br><br> Anyway, I think the answer to my question can be simplified to the following:</p>
<p>Clicking on the command will <code>cd</code> into the folder of the markdown file and run the command.</p>
<p>But this happens in an environment, where PyCharm has added the project folder to <code>PYTHONPATH</code>.</p>
<p>One could say, that <strong>there is no exact console equivalent of the click</strong>,<br> because there is probably no way to figure out from the console, what PyCharm considers to be the project folder.</p>
</details>
<p></p>
</div>
```