Python running on windows or linux

How do I check if I’m running on Windows in Python? [duplicate]

I found the platform module but it says it returns ‘Windows’ and it’s returning ‘Microsoft’ on my machine. I notice in another thread here on stackoverflow it returns ‘Vista’ sometimes. So, the question is, how do implemement?

In a forward compatible way? If I have to check for things like ‘Vista’ then it will break when the next version of windows comes out. Note: The answers claiming this is a duplicate question do not actually answer the question is_windows . They answer the question «what platform». Since many flavors of windows exist none of them comprehensively describe how to get an answer of isWindows .

«There should be one— and preferably only one —obvious way to do it.» Alas, python gives us at least three ways..

5 Answers 5

Specifically for Python 3.6/3.7:

os.name : The name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘java’.

In your case, you want to check for ‘nt’ as os.name output:

There is also a note on os.name :

See also sys.platform has a finer granularity. os.uname() gives system-dependent version information.

The platform module provides detailed checks for the system’s identity.

system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined.

If that isn’t working, maybe try platform.win32_ver and if it doesn’t raise an exception, you’re on Windows; but I don’t know if that’s forward compatible to 64-bit, since it has 32 in the name.

win32_ver(release='', version='', csd='', ptype='') Get additional version information from the Windows Registry and return a tuple (version,csd,ptype) referring to version number, CSD level and OS type (multi/single processor).

But os.name is probably the way to go, as others have mentioned.

For what it’s worth, here’s a few of the ways they check for Windows in platform.py:

if sys.platform == 'win32': #--------- if os.environ.get('OS','') == 'Windows_NT': #--------- try: import win32api #--------- # Emulation using _winreg (added in Python 2.0) and # sys.getwindowsversion() (added in Python 2.3) import _winreg GetVersionEx = sys.getwindowsversion #---------- def system(): """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined. """ return uname()[0] 

Источник

Running python on a Windows machine vs Linux [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

I am interested in learning python but my Linux skills suck. I would like to develop a medium to large scale web application using python and django but afraid the software may not work well on a windows box. Is there a performance difference in running python on Linux vs Windows? Is there anything that I should watch out for when developing the application. Also, I am aware that it is very easy integrating C++ libraries with python. Is this statement still true is the code is on a windows box?

Читайте также:  Sublime text alt linux

Don’t worry. Python is a great language for starters and pros alike. Great news is that it can be installed on Windows so you can learn on a familiar OS. It’s über portable.

You don’t really need ‘skill’ to run linux anymore. Install (K)Ubuntu. It’s easier to install than windows

6 Answers 6

Don’t tell anybody this, but I’ve run python/django on windows. It works all right and the performance hit isn’t any worse than you would expect from windows. I used MySQL and it installed without a problem. I had to grope around to find out how to manage it (no good ol’ sudo /etc/init.d/mysql restart but i eventually found a graphical interface to do what I needed.

@Tim Yates. It was something like services.msc or something. You had to go to the cute little run option on the ‘start’ menu and then type it in. I’m not really a windows person and I’d rather forget that portion of my life.

@TimeYates, @AaronMcSmooth I know for a fact that this can be done through services.msc. You have to press +’r’ to get a run dialog. In there, type in «servcies.msc» (without the quotes) and it should bring you to a list of all services on the system (it also lists if they are currently running and under what conditions they startup). There is a «restart» button which you can use to restart any selected service

but afraid the software may not work well on a windows box.

Your software will work. The Windows OS may not work as you hope. But that’s Windows, not Python.

We develop 100% on Windows. We completely test: Unit test, integration test and user acceptance test on Windows. 100%.

We deploy for production 0% on Windows, 100% on Linux.

We have a few (less than 6) differences in the unit tests that are Windows-specific.

The application has no changes. It works with Apache or not. It works with SQLite or MySQL.

What is the secret for making file paths OS-agnostic? For example, I’m always switching between / and `C:\` in strings.

I’ve been working Py on both Windows and Linux. I favor Linux because of several things:

  1. virtualenvs — once you start working with virtualenvs, there is no turning back.
  2. SHELL — CMD is very frustrating when executing python/management commands in django. Also, you should add python.exe every time :).
  3. ipython works better on Linux.
  4. GeoDjango doesn’t work on Vista/7 last time i checked. I spent 3 days trying to set it up. Just for comparison, i set GeoDjango-able development environment in 20 minutes in Linux.
  5. Linux is free 🙂
  6. Although there is no visible performance impact or incompatibility when working python cross-platform, the benefits of Linux for python development outweigh Windows by a lot. It’s a lot more comfortable and definitely will boost your productivity.
  7. .
Читайте также:  Какие дистрибутивы linux поддерживаются

IMHO Linux is the smart choice for Python development.

Python program is very easily portable. Most of the time your code will work on any platform that have the appropriate version of python.

One point to be aware of though, is file path handling. Linux, Windows, Macs, etc uses different path schemes, so you shouldn’t be handling them as strings; instead use os.path functions to join, split, etc.

There is ultimately some slight performance difference with regard to timing, threading, processing, I/O, but they’re nothing to worry about.

Integrating Python and C++ is easy; the only problem is in the C++ side, i.e. you will have to recompile the C++ code.

Источник

Python running on windows or linux

Super Moderators

buran

really? you don’t have experience with Windows and even refuse to touch anything made by MS (as per your own words) and in the same time you KNOW Windows is not made for that? Let’s be serious. Python is cross-platform.

Super Moderators

nilamo
Last Thursdayist

Python works perfectly fine on either. One of the whole reasons for running on a virtual machine is so the OS doesn’t matter.

As I said I can’t tell more than I have already noticed. There were a lot of questions for win+py+module and how to make them work together on a windows system. If I have to develop something for windows I am going to install one. But for learning purposes. no thanks.

(May-02-2017, 04:38 PM) nilamo Wrote: One of the whole reasons for running on a virtual machine is so the OS doesn’t matter.

I think that the main reason for VMs is different — it is to dynamically build machines for distributed users according to specific demands. The ability to run VM on your PC is a nice bonus — but you need rather strong HW to effectively run VM on your PC.

The issue is — VM itself must run some OS.

Let’s look at this backwards. Python installation has long been a part of Linux distributions. (A sad side effect being corporations’ reluctance to advance to newer versions of Python till they — versions — become standard in Linux distros).

Читайте также:  Kali linux ранние версии

Some of Linux utilities are written in Python — e.g., RedHat’s yum . Python is standard development language at above-mentioned RedHat. It sponsors Python conferences and meetups.

I don’t believe that MS shows such as involvement in Python language.

  • Someone gave you an advice you liked? Test it — maybe the advice was actually bad .
  • Someone gave you an advice you think is bad? Test it before arguing — maybe it was good .
  • You posted a claim that something you did not test works? Be prepared to eat your hat .

Team Member

I just know I’m going to regret getting into this variation of «I hate Microsoft», but here goes. Yes, most Linux (and lets not forget Mac’s and the BSD systems) come with Python pre-installed- part of the OS is written in Python. Windows has nothing dependent on Python, so why install it? Windows comes with the .Net Framework pre-installed, Linux does not. To Windows, Python is just another app, if you want it, install it and it will work. As I said, most of the *nix systems come with it pre-installed, but it is typically not the latest version and does not always come with the «full boat» of modules, as an example on my OpenSuse Linux box, «pip» was not installed. Some included modules have to be modified in order to work correctly with a certain distro, so you can’t just use pip to upgrade, you have to rely on the distro’s repository, which again may not be the latest version. Installing the latest version of Python is a darned sight easier on Windows than on the *nix’s (having installed 3.6.1 on both the Windows 10 and Raspberry Pi).

There are more pro’s and con’s on both sides, so let me just say. neither is «better» than the other, Python is Python and will work on both equally as well.

Just as a side note, if you are new to Python and have a Linux or Mac, you probably have two versions of Python pre-installed, v2.x and v3.x, I encourage you to use the 3.x version (it is typically invoked by typing python3 on the command line. If you are using Windows, I again encourage you to install the latest v3.x.

If it ain’t broke, I just haven’t gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian «Stretch»
Python 3.6.5, IDE: PyCharm 2018 Community Edition

Источник

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