-2

I see the code from nt import in os.py.

I want to see all those functions "with detailed code" in "nt".

For example mkdir() is one of the functions in nt. How can I do this?

Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
A newbie
  • 17
  • 4

3 Answers3

2

the nt module seems to be a bit of a historical artifact, it's basically been merged into posixmodule.c. it should instead be imported via the os module for a very long time (i.e. before Python 2.0).

most of the functions there are native C code, so you'll need to to be relatively proficient in C to be able to understand what's going on. the Extending Python with C or C++ and maybe the Argument Clinic How-To sections in the docs have lots of relevant material

the actual C code behind those methods are in posixmodule.c but its header file also does some parameter marshalling. native methods are defined by PyMethodDef structures, but these can be somewhat obscured in older code bases like CPython

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
1

nt is an OS-specific module, so you really shouldn't be using that. It may only be available on Windows.

Note that os does actually import the right module into its own namespace (this is what from nt import * does in its code when it runs on Windows, whereas it will do from posix import * instead if it runs on Mac OS or Linux).

Therefore, you should actually just look at all the functions of os.

Starting Python in interactive mode on the command line you can do this:

import os
help(os)

If you just want to see all names os has to offer, you can do:

import os
print(dir(os))

If you specifically need help on mkdir, you can also ask for help on this particular function:

import os
help(os.mkdir)
blubberdiblub
  • 4,085
  • 1
  • 28
  • 30
  • Well, you don't need to (and you shouldn't) look into the `nt` module. Just look into `os.mkdir`. Or are you asking for the source code of `mkdir()`? – blubberdiblub Aug 19 '19 at 14:54
  • I just want to see the source code of nt module or where it is. I'm wonder why I can easily saw the source code of os.py module, but I can't even find the nt module in any directory. – A newbie Aug 19 '19 at 15:31
  • As Sam Mason explained in his answer, the `nt` module doesn't exist anymore in modern Python. Instead it gets the `mkdir` function from the `posix` module. But even if the `nt` module existed, it would not be Python code. It would have been written in C and compiled to machine code and Python would make direct use of that. – blubberdiblub Aug 19 '19 at 15:38
0

Most of the IDEs support this options.


Without the need to install anything - you can use ipython for that.

Just start to write your requested module, . and then press tab, and the ipython will display all the documented options. os options

You can find ipython.exe at C:\[your-python]\scripts\ipython.exe.

I'll advice to add this folder to your environment PATH.

YanivGK
  • 893
  • 12
  • 18
  • This isn't really the answer to OP's question. – anand_v.singh Aug 19 '19 at 13:33
  • When I search for nt in python Lib,I just saw activate.bat, Activate.ps1, deactivate.bat, python.exe, pythonw.exe in nt directory, and I can't find any function in these 5 files. Can you say more detail how to use ipython to see the functions with detail code in nt ? thanks – A newbie Aug 19 '19 at 14:21
  • As I mentioned - go to the `scripts` folder (where your python in installed) and then look for `ipython.exe`. If you've installed your python in the last couple of years - this plugin is included. Then - check out the instructions I wrote on the answers above. – YanivGK Aug 19 '19 at 16:23
  • But using ipython I can just saw the functions. I want to see the source code of the nt module. – A newbie Aug 19 '19 at 17:13