Let me explain to you by explaining the outputs of all the commands you mentioned by running them in Windows.
Nothing special here, just importing some modules. Importing numpy
to test the commands.
In [1]: import os
...: import sys
...: import numpy
This gives the current working directory in which your process/program is executing. (detailed tutorial here)
In [2]: os.getcwd()
Out[2]: 'C:\\Users\\<username>'
This tells which file the module is loaded from which file.
In [3]: numpy.__file__
Out[3]: 'C:\\Users\\<username>\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'
The path of the loaded file for the module, it can be a relative path.
In [4]: os.path.dirname(numpy.__file__)
Out[4]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'
The actual path of the loaded file for the module. See the difference from previous in lib
and Lib
. The folder name actually starts from the capital letter.
In [5]: os.path.dirname(os.path.realpath(numpy.__file__))
Out[5]: 'C:\\Users\\Punit Singh\\anaconda3\\Lib\\site-packages\\numpy'
This is the absolute path, you'll see the difference when you'll run this in Linux. It starts with the root folder denoted by \
.
In [6]: os.path.dirname(os.path.abspath(numpy.__file__))
Out[6]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'
[7]
and [8]
respectively give the path and absolute path of the file which is currently executing, here sys.argv[0]
returns the file which is currently executing, since currently, I am running ipython, it is ipython.exe shown in [9]
. (detailed tutorial here)
In [7]: os.path.dirname(sys.argv[0])
Out[7]: 'C:\\Users\\<username>\\anaconda3\\Scripts'
In [8]: os.path.abspath(os.path.dirname(sys.argv[0]))
Out[8]: 'C:\\Users\\<username>\\anaconda3\\Scripts'
In [9]: sys.argv[0]
Out[9]: 'C:\\Users\\<username>\\anaconda3\\Scripts\\ipython'
This gives the absolute path of the executable binary for the Python interpreter (documentation here)
In [10]: sys.executable
Out[10]: 'C:\\Users\\Punit Singh\\anaconda3\\python.exe'
Now coming to your requirement, I understand that you need to access files where this ipython.exe
as shown in [9]
is located, so you can use commands mentioned in [7]
or [8]
.