In your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
Answer from elo80ka on Stack OverflowIn your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
If you have Python in your environment variable then you can use the following command in cmd or powershell:
where python
or for Unix enviroment
which python
command line image :

So I've been having trouble with Python and I want to know which installation I'm using (I have two user profiles on Windows, my old one and the one I made explicitly to code in, and they both have Python installed).
I tried looking it up, but it only led to this madness you see below:
Microsoft Windows [Version 10.0.19043.1348]
(c) Microsoft Corporation. All rights reserved.
C:\Users\willi>!type python
'!type' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\willi>type python
The system cannot find the file specified.
C:\Users\willi>type Python
The system cannot find the file specified.
C:\Users\willi>Python type
Python: can't open file 'C:\Users\willi\type': [Errno 2] No such file or directory
C:\Users\willi>Python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> .quit
File "<stdin>", line 1
.quit
^
SyntaxError: invalid syntax
>>> quit
Use quit() or Ctrl-Z plus Return to exit
>>> quit()
C:\Users\willi>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> !type Python
File "<stdin>", line 1
!type Python
^
SyntaxError: invalid syntax
>>> type
<class 'type'>
>>> type python
File "<stdin>", line 1
type python
^
SyntaxError: invalid syntax
>>> type Python
File "<stdin>", line 1
type Python
^
SyntaxError: invalid syntax
>>> type(Python)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Python' is not defined
>>>How do I view Python's location in command prompt?
First question:
which python though its usually /usr/bin/python for the 2.7
Second question:
From a terminal & python2.7: python2.7 yourfile.py.
Simailarly for 3.2: python3.2 yourfile.py though 3.2 isn't installed by default. (You can apt-get install python3.2.)
What python yourfile.py will do depends on which alternative is used for your python interpreter. You can change that by issuing update-alternatives python as root (or by using su).
Third question:
Environment variables are shell dependent, though you can write them out with echo $variable and set them with variable=value (from bash). The search path is simply called PATH and you can get yours by typing echo $PATH.
I hope this was helpful.
If you want to find the location of a program you can just use whereis <program>.
In your case run:
whereis python2.7
whereis python3.2
For finding every file that apt-get has copied for installation use:
dpkg -S python2.7
dpkg -S python3.2
But maby it is recommend to save it in a textfile, because the output is to large.
dpkg -S python2.7 >log.txt
gedit log.txt
for running .py file with python 3.2
python3.2 <file.py>
My understanding of np.where() was so far as sort of an arg container for .loc. However in the example below, .loc works, but not when np.where is used as arg. Why is that happening?
df=pd.DataFrame({'Name':['Tom', 'Mia', 'Sam'], 'Age':[15, 26, 32]}, index=['A','B','C'])
print(df.loc[(df['Age']>28)&(df['Name'].str.startswith('S')])
k=np.where((df['Age']>28)&(df['Name'].str.startswith('S'))
print(df.loc[k])
How do they achieve internally that you are able to pass something like x > 5 into a method?
The short answer is that they don't.
Any sort of logical operation on a numpy array returns a boolean array. (i.e. __gt__, __lt__, etc all return boolean arrays where the given condition is true).
E.g.
x = np.arange(9).reshape(3,3)
print x > 5
yields:
array([[False, False, False],
[False, False, False],
[ True, True, True]], dtype=bool)
This is the same reason why something like if x > 5: raises a ValueError if x is a numpy array. It's an array of True/False values, not a single value.
Furthermore, numpy arrays can be indexed by boolean arrays. E.g. x[x>5] yields [6 7 8], in this case.
Honestly, it's fairly rare that you actually need numpy.where but it just returns the indicies where a boolean array is True. Usually you can do what you need with simple boolean indexing.
Old Answer it is kind of confusing. It gives you the LOCATIONS (all of them) of where your statment is true.
so:
>>> a = np.arange(100)
>>> np.where(a > 30)
(array([31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99]),)
>>> np.where(a == 90)
(array([90]),)
a = a*40
>>> np.where(a > 1000)
(array([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99]),)
>>> a[25]
1000
>>> a[26]
1040
I use it as an alternative to list.index(), but it has many other uses as well. I have never used it with 2D arrays.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html
New Answer It seems that the person was asking something more fundamental.
The question was how could YOU implement something that allows a function (such as where) to know what was requested.
First note that calling any of the comparison operators do an interesting thing.
a > 1000
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True`, True, True, True, True, True, True, True, True, True], dtype=bool)`
This is done by overloading the "__gt__" method. For instance:
>>> class demo(object):
def __gt__(self, item):
print item
>>> a = demo()
>>> a > 4
4
As you can see, "a > 4" was valid code.
You can get a full list and documentation of all overloaded functions here: http://docs.python.org/reference/datamodel.html
Something that is incredible is how simple it is to do this. ALL operations in python are done in such a way. Saying a > b is equivalent to a.gt(b)!