the file structure is as follows:
$ tree .
.
main.py
os.py
platform.py
main.py:
import platform
import os
import sys
print("Now in main, Start`enter code here` to Exec following")
print(os.getcwd())
print(sys.modules["platform"].__file__)
print(platform.processor())
os.py:
print("Now in os.py module")
platform.py:
print("Now in platform.py module")
when I run python main.py
, the result is as follows:
$ python main.py
Now in platform.py module
Now in main, Start to Exec following
/Users/michael/Code/00-Temp/pkg_import
/Users/michael/Code/00-Temp/pkg_import/platform.py
Traceback (most recent call last):
File "main.py", line 9, in <module>
print(platform.processor())
AttributeError: module "platform" has no attribute "processor"
question 1: os
and platform
are both built-in modules. When import os
, they go to the built-in module by default, but import platform
gets the plarform
in the same directory. Why does this happen?
question 2: for the same script, I ran main.py directly in Pycharm, but reported this error:
/Users/michael/anaconda3/bin/python /Users/michael/Code/00-Temp/pkg_import/main.py
Now in os.py module
Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
File "/Users/michael/anaconda3/lib/python3.7/site.py", line 570, in <module>
main()
File "/Users/michael/anaconda3/lib/python3.7/site.py", line 547, in main
known_paths = removeduppaths()
File "/Users/michael/anaconda3/lib/python3.7/site.py", line 126, in removeduppaths
dir, dircase = makepath(dir)
File "/Users/michael/anaconda3/lib/python3.7/site.py", line 91, in makepath
dir = os.path.join(*paths)
AttributeError: module "os" has no attribute "path"
Process finished with exit code 1