PyCharm imports Modules BEFORE my own code runs

已回答

For the sake of demonstration I tried to show what happens, when a self created module conflicts with existing modules from stdlib.

I create a file “runner.py” with the code:

import random
print(random.random())

next to the first file I create a file “random.py” with no content (empty).

When running “runner.py” from OS-console I get the expected Error (no function “random” in module “random”).

Using the same Code in PyCharm I can see that Code-Inspection predicts the very same Error (red highlighted code), but when running it I get a random number printed (as if the original module random was loaded BEFORE my own code, making my import statement pointless and using the function from the original module).
 


When turning the logic around, similarly confusing stuff happens:
I add a function “hello()” to my module “random.py”. The function just prints “hello”. In the “runner.py” I use now the following code:

import random
random.hello()

Running this in the OS-console I get the expected “hello” Output.

Opening the example in PyCharm I get NO ISSUES reported from code-inspection but when running it from PyCharm I get an exception complaining that module “random” doesnt have a function called “hello”.

PyCharm breaks the documented Python behaviour. More specifically it behaves fundamentally different to the behaviour of the same  code using the same interpreter on the same OS/machine. I am aware that using module-names existing in stdlib is bad choice, but for the sake of understanding whats going on it still feels odd. 

So even the Code-inspection of PyCharm confirms the expected and documented Python behaviour, while running the code from PyCharm things deviate.

Is there an explanation what happens and why?
Is there potentially a way to deactivate this “feature”?
 

 

 

 

 

 

 

0

Looks like a path-order mismatch between static inspection  and runtime import resolution. PyCharm prepends the project root directory to PYTHONPATH, which can change which random gets found first compared to running in your OS terminal.

Run this once from your OS console and once from PyCharm; you’ll see the difference

import sys, random
print("random imported from:", getattr(random, "__file__", "<builtin>"))
print("sys.path[0] =", sys.path[0])

If you want, you can set the Working directory to the folder that contains runner.py and random.py and Uncheck: “Add content roots to PYTHONPATH” and “Add source roots to PYTHONPATH” on the run config inside PyCharm. Then PyCharm will follow the same import resolution you see in the terminal

Although we recommend that if you want to experiment with shadowing, do it explicitly (e.g., package it under mypkg/random.py and from mypkg import random)

0

The problem is not where to look for modules!

 

sys.path[0] is the same in both cases (as expected) (its the folder where runner.py resides and where the custom module random.py lives as well). Besides built-in modules sys.path[0] is the first place (and the same in both cases) where to look for to be imported modules and since random is not built-in in both cases my custom module should be the first one found. Thats the documented behaviour of Python and thats also what happens in all cases but PyCharm.

this code 

import sys

print('random' in sys.modules)

 

outputs True, when run from PyCharm but False, when run from anywhere else


The other entries in sys.path are different in the two cases (as expected) but having the first entry the same (as expected) I expect only the custom module to be used in both cases. 
 

0

请先登录再写评论。