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

130 Views Asked by At

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.
PyCharm shows a green triangle next to it.
When I click on it, the output tells me, that folder is my CWD.

enter image description here

This is the desired behavior. (Above all, DELETE_ME is created in folder.)
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.
The closest equivalent I have found is to do python -m run in folder.
(While running the whole command in folder creates a ModuleNotFoundError.)

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

enter image description here

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

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

3

There are 3 best solutions below

0
Watchduck On BEST ANSWER

The answer and comments by Ahmed AEK contain all the important facts.
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.

1
Nikita Karamov On

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

8
Ahmed AEK On

a console equivalent in bash would be

(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.