Python check if running on windows

Python: What OS am I running on?

What do I need to look at to see whether I’m on Windows or Unix, etc?

26 Answers 26

The output of platform.system() is as follows:

  • Linux: Linux
  • Mac: Darwin
  • Windows: Windows

Dang — lbrandy beat me to the punch, but that doesn’t mean I can’t provide you with the system results for Vista!

. and I can’t believe no one’s posted one for Windows 10 yet:

For the record here’s the results on Mac:

Sample code to differentiate OS’s using python:

Short Story

Use platform.system() . It returns Windows , Linux or Darwin (for OSX).

Long Story

There are 3 ways to get OS in Python, each with its own pro and cons:

Method 1

How this works (source): Internally it calls OS APIs to get name of the OS as defined by OS. See here for various OS-specific values.

Pro: No magic, low level.

Con: OS version dependent, so best not to use directly.

Method 2

How this works (source): Internally it checks if python has OS-specific modules called posix or nt.

Pro: Simple to check if posix OS

Con: no differentiation between Linux or OSX.

Method 3

How this works (source): Internally it will eventually call internal OS APIs, get OS version-specific name like ‘win32’ or ‘win16’ or ‘linux1’ and then normalize to more generic names like ‘Windows’ or ‘Linux’ or ‘Darwin’ by applying several heuristics.

Pro: Best portable way for Windows, OSX and Linux.

Con: Python folks must keep normalization heuristic up to date.

How do I check the operating system in Python?

I want to check the operating system (on the computer where the script runs).

I know I can use os.system(‘uname -o’) in Linux, but it gives me a message in the console, and I want to write to a variable.

It will be okay if the script can tell if it is Mac, Windows or Linux. How can I check it?

5 Answers 5

sys.platform has finer granularity than sys.name .

For the valid values, consult the documentation.

If you want to know on which platform you are on out of «Linux», «Windows», or «Darwin» (Mac), without more precision, you should use:

The platform.system function uses uname internally.

You can get a pretty coarse idea of the OS you’re using by checking sys.platform .

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

Читайте также:  Windows 10 не работает подсветка экрана

There’s also psutil if you want to do more in-depth inspection without wanting to care about the OS.

More detailed information are available in the platform module.

How can I know if my python script is running? (using Cygwin or Windows shell)

I have a python script named sudoserver.py that I start in a CygWin shell by doing:

I am planning to create a shell script (I don’t know yet if I will use Windows shell script or a CygWin script) that needs to know if this sudoserver.py python script is running. But if I do in CygWin (while sudoserver.py is running):

and in Windows shell:

So it seems I have no info about the .py file being executed. All I know is that python is running something.
The -l (long) option for ‘ps’ on CygWin does not find my .py file. Nor does it the /v (verbose) switch at tasklist .
What should be the appropriate shell (Windows or CygWin shell would enough; both if possible would be fine) way to programmatically find if an specific python script is executing right now?

NOTE: The python process could be started by another user. Even from a user not logged in a GUI shell, and, even more, the «SYSTEM» (privileged) Windows user.

2 Answers 2

It is a limitation of the platform.

You probably need to use some low level API to retrieve the process info. You can take a look at this one: Getting the command line arguments of another process in Windows

You can probably use win32api module to access these APIs.

(Sorry, away from a Windows PC so I can’t try it out)

Since sudoserver.py is your script, you could modify it to create a file in an accessible location when it starts and to delete the file when it finishes. Your shell script can then check for the existence of that file to find out if sudoserver.py is running.

Thanks to the commenters who suggested that while the presence or absence of the file is an unreliable indicator, a file’s lock status is not.

I wrote the following Python script testlock.py :

. and ran it in a Cygwin console window on my Windows PC. At the same time, I had another Cygwin console window open in the same directory.

First, after I started testlock.py :

. then after I had shut down testlock.py by using Ctrl-C :

Thus, it appears that Windows is locking the file while the testlock.py script is running but it is unlocked when it is stopped with Ctrl-C . The equivalent test can be carried out in Python with the following script:

. which correctly reports:

. when testlock.py is running but successfully removes the locked file when testlock.py has been stopped with a Ctrl-C .

Note that this approach works in Windows but it won’t work in Unix because, according to the Python documentation:

On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

A platform-independent solution using an additional Python module FileLock is described in Locking a file in Python.

Читайте также:  Windows core когда использовать

How can I find where Python is installed on Windows?

I want to find out my Python installation path on Windows. For example:

How can I find where Python is installed?

18 Answers 18

In your Python interpreter, type the following commands:

Also, you can club all these and use a single line command. Open cmd and enter following command

If you have Python in your environment variable then you can use the following command in cmd:

or for Unix enviroment

command line image :

It would be either of

  • C:\Python36
  • C:\Users\(Your logged in User)\AppData\Local\Programs\Python\Python36

If you need to know the installed path under Windows without starting the python interpreter, have a look in the Windows registry.

Each installed Python version will have a registry key in either:

In 64-bit Windows, it will be under the Wow6432Node key:

On my windows installation, I get these results:

(You can also look in sys.path for reasonable locations.)

or try using (in cmd )

In the sys package, you can find a lot of useful information about your installation:

I’m not sure what this will give on your Windows system, but on my Mac executable points to the Python binary and exec_prefix to the installation root.

You could also try this for inspecting your sys module:

If you have the py command installed, which you likely do, then just use the —list-paths argument to the command:

Installed Pythons found by py Launcher for Windows
-3.8-32 C:\Users\cscott\AppData\Local\Programs\Python\Python38-32\python.exe *
-2.7-64 C:\Python27\python.exe

The * indicates the currently active version for scripts executed using the py command.

If You want the Path After successful installation then first open you CMD and type python or python -i

It Will Open interactive shell for You and Then type

Hit enter and you will get path where your python is installed .

To know where Python is installed you can execute where python in your cmd.exe.

You can search for the «environmental variable for you account». If you have added the Python in the path, it’ll show as «path» in your environmental variable account.

but almost always you will find it in «C:\Users\%User_name%\AppData\Local\Programs\Python\Python_version«

the ‘AppData‘ folder may be hidden, make it visible from the view section of toolbar.

If anyone needs to do this in C# I’m using the following code:

Go to C:\Users\USER\AppData\Local\Programs\Python\Python36 if it is not there then open console by windows+^R Then type cmd and hit enter type python if installed in your local file it will show you its version from there type the following import os import sys os.path.dirname(sys.executable)

This worked for me: C:\Users\Your_user_name\AppData\Local\Programs\Python

My currently installed python version is 3.7.0

Hope this helps!

if you still stuck or you get this

simply do this replace 2 \ with one

I installed 2 and 3 and had the same problem finding 3. Fortunately, typing path at the windows path let me find where I had installed it. The path was an option when I installed Python which I just forgot. If you didn’t select setting the path when you installed Python 3 that probably won’t work — unless you manually updated the path when you installed it. In my case it was at c:\Program Files\Python37\python.exe

Читайте также:  Linux запуск приложения свернутым

If you use anaconda navigator on windows, you can go too enviornments and scroll over the enviornments, the root enviorment will indicate where it is installed. It can help if you want to use this enviorment when you need to connect this to other applications, where you want to integrate some python code.

Not the answer you’re looking for? Browse other questions tagged python windows path or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Python 3 installation on windows running from command line

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line «python3», like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.

EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command «python».

4 Answers 4

the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command «python».

To run Python 2 executable:

To run Python 3 executable:

where py is a Python launcher that is bundled with your Python 3 installation.

py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:

and the correct python version is used automatically — you don’t need to specify the Python version on the command-line explicitly.

is there a particular reason why Python 3.x is not installed on Windows to run default with the command line «python3», like it does on Mac OSX and Linux?

OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.

There is no Python on Windows by default. And therefore any version that you’ve installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.

Is there some kind of way to configure Python so that it runs like this?

Оцените статью