(stupid?) curiosity question

已回答

I have a fresh install of PyCharm 2024.2.1, using Python 3.12

  1. I create a new project.
  2. I create one file in the top project folder called “test.py” with the content

    import random
    print('test running')
  3. I create one file in the top project folder called “random.py” 

    print('random loaded')
    
  4. I am aware that the file random.py would shadow the existing module from stdlib. But I never use/import it anyways.
  5. When running test.py I get the expected

    random loaded
    test running
  6. I create one file in the top project folder called “test2.py” with the content

    print("test2 running")
  7. When running test.py from PyCharm  I am surprised to see

    random loaded
    test2 running
  8. running the same code from commandline delivers the expected

    test2 running

    PyCharm seems to run my module (without any complaints)

    my sys.path looks as expected, as far as I can tell

0

forgot the Question:

 

WHY? / HOW?

0

Hi Marcoschuler, by default PyCharm extends sys.path by additional entries under the hood which have imports of random somewhere deep inside.

This behavior can be disabled by toggling off "Add content roots to PYTHONPATH" option in the given run configuration:

0

thank you, that solved my understanding issue

0

请先登录再写评论。