Python在我的机器上,我只是不知道在哪里,如果我在终端中输入Python,它会打开Python 2.6.4,这不是在它的默认目录中,肯定有一种方法可以从这里找到它的安装位置?
当前回答
对于Windows CMD,执行:where python . exe
Windows PowerShell: Get-Command python
其他回答
看看sys.path:
>>> import sys
>>> print(sys.path)
Sys有一些有用的东西:
$ python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'c:\\Python26\\python.exe'
>>> sys.exec_prefix
'c:\\Python26'
>>>
>>> print '\n'.join(sys.path)
c:\Python26\lib\site-packages\setuptools-0.6c11-py2.6.egg
c:\Python26\lib\site-packages\nose-1.0.0-py2.6.egg
C:\Windows\system32\python26.zip
c:\Python26\DLLs
c:\Python26\lib
c:\Python26\lib\plat-win
c:\Python26\lib\lib-tk
c:\Python26
c:\Python26\lib\site-packages
c:\Python26\lib\site-packages\win32
c:\Python26\lib\site-packages\win32\lib
c:\Python26\lib\site-packages\Pythonwin
c:\Python26\lib\site-packages\wx-2.8-msw-unicode
平台独立的解决方案在一条线上
Python 2:
python -c "import sys; print sys.executable"
Python 3:
python -c "import sys; print(sys.executable)"
如果你使用的是windows操作系统(我使用的是windows 10),只需输入
where python
在命令提示符(CMD)中
它将显示您所安装的目录。
在类unix系统上,您应该能够键入哪个python,这将打印出python的路径。Windows命令提示符中的等效语句是where python, Windows Powershell中的Get-Command python。
另一种(跨平台)方法是将其输入IDLE或REPL(在终端中输入python):
import re
re.__file__
或者在你的终端上写一行:
python -c "import re; print(re.__file__)"
这将打印re模块的路径,从而显示python命令指向的位置。你可以放置你知道已经安装的任何其他模块,路径将指向该模块,也为你提供python的路径。