2

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.

user2153235
  • 388
  • 1
  • 11

2 Answers2

2

No, sys does not import os. However, os does import sys.

>>> 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 from an autogenerated C version, rather than from the os.py file. The sys module will likely be loaded by a BuiltinImporter, and is actually compiled into the interpreter (along with a handful of others, 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.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Yes, I noticed that `os` loads `sys` from [one of my citations](https://thomas-cokelaer.info/tutorials/python/module_os.html). It is in the `os.*` namespace. For me, `python3` wasn't recognized, but since my Conda environment is for Python 3.9, I just issued `python -v -c`. A lot of output! I used Cygwin's `script` command to run the CMD command that launches the Conda prompt, thus capturing the terminal session for perusal with Vim. The words `sys` and `os` occur in many contexts, but I only see what looks like importation of `os`. Thanks for clarifying what importing does in this situation. – user2153235 Jun 29 '23 at 00:38
  • Being a newbie to Python, I didn't quite follow your paragraph about freezing modules, but the rest of your answer was very helpful (and even the parts I don't follow are probably helpful to others). – user2153235 Jun 29 '23 at 00:41
-1

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
Zero
  • 1,807
  • 1
  • 6
  • 17
  • Egad! Looks like the [page](https://stackoverflow.com/questions/30483246/how-can-i-check-if-a-module-has-been-imported/30483269) I cited in my question for testing the presence of a module is mostly unreliable. [One specific answer](https://stackoverflow.com/a/51637094/2153235) on that page is more reliable and corroborates with your answer. – user2153235 Jun 28 '23 at 23:28
  • @user2153235 It really depends on what exactly you're wanting to test. The first shows how to check if a module has been imported at all from anywhere, and the original question was for anywhere in the code, not in a specific module. – Carcigenicate Jun 28 '23 at 23:32
  • I'm just getting to know Python. I was not aware that the importation of modules are specific to certain scopes. So far, I've mostly been exploring using the REPL command line. I have to admit, I don't quite get the distinction between your reference to "from anywhere" versus "from anywhere in the code".\ – user2153235 Jun 28 '23 at 23:37
  • @user2153235 There's no difference, which was my point. The first question you linked to isn't unreliable, it's just not the correct question to look at for your case if you only want to check the current module. – Carcigenicate Jun 28 '23 at 23:40
  • So are module importations specific to scopes (like nonglobal variables are). Does this mean that if module "x" imports module "y", then "y" is only visible to the code in "x"? – user2153235 Jun 28 '23 at 23:47
  • What makes you think `sys` uses the `os` module? – wim Jun 29 '23 at 00:13