1

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

Watchduck
  • 1,076
  • 1
  • 9
  • 29

3 Answers3

2

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.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • By `new_work_dir` you mean my `folder`? The command I click on is `python -m path.to.my_script`. You write, that in the console equivalent this becomes `path/to/python -m my_script`. Do you literally mean, that `path.to.my_script` is truncated to `my_script`? – Watchduck Aug 10 '23 at 21:04
  • My question was, what happens when I click on `python -m path.to.folder.run`. Is your answer, that it means `cd path/to/folder` and then `python -m run`? (That does indeed work, and have the same result. But is it really what happens?) – Watchduck Aug 10 '23 at 21:11
  • @Watchduck `a001_misc.b006_cwd.folder.run` is parsed by the interpreter, so `examples_py` should be on your `PYTHONPATH` or `PATH`, the answer is now corrected without the extra noise. i initially misunderstood the question, – Ahmed AEK Aug 10 '23 at 21:36
  • Thanks. So my summary of your answer in the last comment is basically correct? – Watchduck Aug 10 '23 at 21:57
  • @Watchduck yes, that ould be correct. – Ahmed AEK Aug 10 '23 at 21:59
  • I am still surprised, that in the _clicking_ world `python -m path.to.script` and `python -m script` do the same thing. Because in the _console_ world only the short version works from `folder`, while the long version creates a `ModuleNotFoundError`. Is there something worth understanding behind this difference, or does it boil down to "PyCharm does it this way"? – Watchduck Aug 10 '23 at 22:12
  • 1
    @Watchduck if `examples_py` is your current "pycharm project folder" then pycharm has added it to your `PYTHONPATH` before calling the interpreter. – Ahmed AEK Aug 10 '23 at 22:14
  • This seems to be the core of the answer to my question. I have created [my own answer](https://stackoverflow.com/a/76879807/2307570) based on this. – Watchduck Aug 10 '23 at 23:10
0

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

Nikita Karamov
  • 517
  • 1
  • 5
  • 18
  • Somehow this command lacks context. When I run it, the CWD is where I run it. For PyCharm the CWD is `folder`. But I can not run it in `folder`, because that creates a `ModuleNotFoundError`. So where does PyCharm run this? And how can I do the same? Invalidating the cache did not work. This even happens when I add the same line to a new markdown file. – Watchduck Aug 10 '23 at 14:50
0

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.

Watchduck
  • 1,076
  • 1
  • 9
  • 29