英文:
Python: Does importing "sys" module also import "os" module?
问题
I am following this tutorial to see why Python doesn't recognize an environment variable that was set at the Conda prompt (using CMD syntax, as this is an Anaconda installation on Windows 10).
It requires the os
module, and as part of my getting familiar with Python, I decided to test whether os
was already imported. Testing the presence of a module requires the sys
module (as described here).
Strangely, right after importing sys
, I found that os
was imported without me having to do so. I find this odd, as most of my googling shows that you have to import them individually, e.g., here.
Does importing sys
also impart os
, as it seems to? If so, why is it common to import both individually?
I can't test for the presence of os
before importing sys
, as I need sys
to test for the presence of modules.
Here is the code that shows the apparent presence of os
from importing sys
, formatted for readability. It starts from the Conda prompt in Windows. The Conda environment is "py39", which is for Python 3.9:
(py39) C:\Users\User.Name > python
Python 3.9.16 (main, Mar 8 2023, 10:39:24)
[MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits", or "license"
for more information.
>>> import sys
>>> "os" in sys.modules
True
Afternote: Thanks to Zero's answer, I found this code to be more what I'm looking for. After loading sys
, the appropriate test is ( 'os' in sys.modules ) and ( 'os' in dir() )
:
(py39) C:\Users\User.Name > python
'os' in dir() # False
import sys
'os' in sys.modules , 'os' in dir() # (True, False)
( 'os' in sys.modules ) and ( 'os' in dir() ) # False
import os
'os' in sys.modules , 'os' in dir() # (True, True)
( 'os' in sys.modules ) and ( 'os' in dir() ) # True
sys.modules
shows whether the module has been imported anywhere (presumably in the code that the Python interpreter has executed) while dir()
indicates whether the module name is in the current namespace. Thanks to Carcigenicate for clarifying this point, and I hope that I understood it properly.
英文:
I am following this tutorial to see why Python doesn't recognize an environment variable that was set at the Conda prompt (using CMD syntax, as this is an Anaconda installation on Windows 10).
It requires the os
module, and as part of my getting familiar with Python, I decided to test whether os
was already imported. Testing the presence of a module requires the sys
module (as described here).
Strangely, right after importing sys
, I found that os
was imported without me having to do so. I find this odd, as most of my googling shows that you have to import them individually, e.g., here.
Does importing sys
also impart os
, as it seems to? If so, why is it common to import both individually?
I can't test for the presence of os
before importing sys
, as I need sys
to test for the presence of modules.
Here is the code that shows the apparent presence of os
from importing sys
, formatted for readability. It starts from the Conda prompt in Windows. The Conda environment is "py39", which is for Python 3.9:
(py39) C:\Users\User.Name > python
Python 3.9.16 (main, Mar 8 2023, 10:39:24)
[MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
>>> import sys
>>> "os" in sys.modules
True
Afternote: Thanks to Zero's answer, I found this code to be more what I'm looking for. After loading sys
, the appropriate test is ( 'os' in sys.modules ) and ( 'os' in dir() )
:
(py39) C:\Users\User.Name > python
'os' in dir() # False
import sys
'os' in sys.modules , 'os' in dir() # (True, False)
( 'os' in sys.modules ) and ( 'os' in dir() ) # False
import os
'os' in sys.modules , 'os' in dir() # (True, True)
( 'os' in sys.modules ) and ( 'os' in dir() ) # True
sys.modules
shows whether the module has been imported anywhere (presumably in the code that the Python interpreter has executed) while dir()
indicates whether the module name is in the current namespace. Thanks to Carcigenicate for clarifying this point, and I hope that I understood it properly.
答案1
得分: 2
不,`sys` 不会导入 `os`。然而,[`os` 会导入 `sys`](https://github.com/python/cpython/blob/ce091c96cfb9a5cd6c7120dc077606da993680b4/Lib/os.py#L26)。
```python
>>> import os
>>> os.sys
<module 'sys' (built-in)>
实际的 os
模块对象在典型的安装中可能会被冻结,也就是说它将被一个 FrozenImporter
从一个自动生成的 C 版本中找到,而不是从 os.py
文件中。sys
模块可能会被一个 BuiltinImporter
载入,并且实际上被编译进解释器中(与另外一些模块一起,目前不包括 os
)。
请注意,它们两者在解释器启动时都会被导入,如下所示:
python3 -v -c ''
当你从交互式解释器中导入 sys
和/或 os
时,你只是将它们绑定到了你的本地命名空间中 - 这两个模块已经完全加载,并且不会再次被导入。
<details>
<summary>英文:</summary>
No, `sys` does not import `os`. However, [`os` does import `sys`](https://github.com/python/cpython/blob/ce091c96cfb9a5cd6c7120dc077606da993680b4/Lib/os.py#L26).
>>> import os
>>> os.sys
<module 'sys' (built-in)>
The actual `os` module object will likely be frozen on a typical installation, i.e. it will be found by a [`FrozenImporter`](https://docs.python.org/3/library/importlib.html#importlib.machinery.FrozenImporter) from an [autogenerated C version](https://github.com/python/cpython/blob/3fb7c608e5764559a718ce8cb81350d7a3df0356/Python/frozen.c#L122), rather than from the `os.py` file. The `sys` module will likely be loaded by a [`BuiltinImporter`](https://docs.python.org/3/library/importlib.html#importlib.machinery.BuiltinImporter), and is actually compiled into the interpreter (along with [a handful of others](https://docs.python.org/3/library/sys.html#sys.builtin_module_names), not currently including `os`).
Note that they're _both_ imported during interpreter startup, as you can see by executing:
python3 -v -c ''
When you import `sys` and/or `os` from an interactive interpreter, all you do is bind the names in your local namespace - both modules have already been fully loaded and will not be imported again.
</details>
# 答案2
**得分**: -1
`sys` 使用了 `os` 模块,但没有将其导入到您的代码中。您可以通过以下代码进行确认:
```python
import sys
然后您可以使用以下代码来获取当前工作目录:
import os
os.getcwd()
英文:
sys
uses the os
module, but doesn't import it to your code. You can confirm it through the code.
In [1]: import sys
In [2]: os.getcwd()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In [2], line 1
----> 1 os.getcwd()
NameError: name 'os' is not defined
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论