Run Python Shell from Windows CMD (bypass cygwin) - python

I have cygwin installed on my win 7 pc. When I'm running the python shell, I get the OS name as posix (which is expected) and many DOS commands don't work using os.system("DOS command").
Is there a way I can avoid entering the posix shell from Windows command prompt?

You need to run the native Windows Python interpreter rather than the Cygwin port.
Assuming you already have both installed on your system, check your PATH setting to confirm the native Windows edition comes first.
If this doesn't work for you, and you'd RATHER run the Cygwin port, then you are going to have to avoid os.system() calls, and instead switch to using the new subprocess package. Give a read over http://docs.python.org/library/subprocess.html

Related

How to make a GUI for command-line application in Python?

I am trying to make a command-line application using Python.
I have developed the program, but I see that my program gets executed in cmd.exe, which seems to be the default for that kind of programs.
But I would like a finer GUI, like that of Windows Terminal. But the problem is that, Windows Terminal is not installed in all Windows computers as is cmd.exe.
Because of this I would like to request you that to suggest any methods to make the GUI or a method to bundle up Windows Terminal with my Python program.
So first we need to understand some terminology. You compare Windows Terminal and cmd.exe, but those are actually two entirely different types of applications. CMD is the old Windows shell. It runs inside a terminal emulator (a.k.a. terminal). There are multiple terminals that it can run under in Windows:
By default in Windows 10, it runs in the legacy Windows Console Host. This is the nearly 30 year (maybe more) old terminal for Windows.
CMD can also run inside Windows Terminal, the replacement terminal by Microsoft that is available to install in Windows 10 or Windows 11. Under Windows 11, it can become the default terminal for Windows.
When you ask Windows to run a console/terminal app, like your Python CLI app, Windows always runs it in its default terminal -- Under Windows 10, that is always the Windows Console Host. Under Windows 11, it may be either Windows Console Host or Windows Terminal, depending on the user configuration.
But there are also multiple other terminals that can run your CLI Python app in Windows:
Some of the more popular are:
ConEmu
Cmder
MobaXterm
You can find others listed in this blog post.
So it's really not that your app runs in CMD, but that your app runs in Python (the interpreter), which is running under CMD (the shell), which is running under Windows Console Host (the default terminal emulator).
Note that Python can also run under other shells (PowerShell, Linux shells under WSL, Nushell, etc.). These shells can also run in any of the terminals above. Or your Python CLI app can run as the top-level inside a terminal.
Ok, now that we've established some basics ...
But I would like a finer GUI, like that of Windows Terminal.
It's a bit unclear what you mean by that, but there may be other solutions than trying to package Windows Terminal. Both the legacy Console and Windows Terminal should have similar capabilities for your application. Windows Terminal adds things like tabs for the user, but you won't have control over that in your application. Both Console Host and WT can be configured with the same Windows fonts.
One thing you can do in Windows Terminal that you can't in Console Host is define a Profile for your application, including the themes, background image, etc. that you want it to run under. Perhaps that's what you are asking for, but you don't specify that those capabilities.
suggest any methods to make the GUI
If you are looking to create a GUI in Python, consider building it using a GUI Framework rather than as a CLI application. You can even write a terminal emulator in Python, but that would likely be a bit overkill for this.
or a method to bundle up Windows Terminal with my Python program.
That's probably not a good idea. Windows Terminal works best when it is installed as a Microsoft Store app (a.k.a. UWP, Modern, etc.). It is not possible to bundle Microsoft Store apps with another application; just like you can't install another Apple App Store app automatically from another on iOS. It's also possible to install/run Windows Terminal as a traditional .exe, but that's not the default (or supported) way.
You might consider the following:
Have your main Python app be a "launcher" that checks to see if Windows Terminal is installed, perhaps by looking for the existing of the wt.exe.
If Windows Terminal is installed:
Check for the existence of your applications JSON fragment in the appropriate directory (see link). Install it if it doesn't exist.
Launch your application by calling something like wt.exe new-tab --profile <your_app_profile> to start or open a new tab in Windows Terminal with the profile you defined in your JSON fragment.
If Windows Terminal is not installed:
Recommend that the user install it. Open the Microsoft Store link to it.
If they choose not to, your launcher should still launch the CLI portion of the app -- Windows will use the default terminal, which should be Windows Console Host.
Provide the instructions for running the CLI portion by itself (without the launcher) so that users of other terminal emulators can run it inside their preferred environment.

How to make the equivallent of .bat for mac and how to install python libraries on mac

I have this file.py:
import os
os.system("pip install pip")
os.system("pip install selenium")
How do I make it work for MAC and what is te equivallent of a .bat file in MAC to execute the file.py.
Your file.py script will generally work fine on Mac as long as the environment the script is running in is set up right. Most notably, the pip executable has to be findable via the current PATH variable. You might benefit by looking at the subprocess module, which is an alternative API for running external commands. It is a more robust mechanism for doing so.
The equivalent of a .BAT file is a shell script. You have a choice as to which shell to use to run the script. I think the most common source is the Bash shell. It is often the case that you use whatever shell is running at your command prompt. This functionality is generally much more general and flexible than a .BAT file is on Window. See this link for a discussion of many of the issues:
https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html
A shell script can just be one or more commands that you might run in your Terminal. For example, to run test.py at a Terminal prompt, you'd do this:
> python test.py
The simplest equivalent in a shell script would be the same thing:
python test.py
A script that looks like this is run by whatever shell executes the shell script. What is more usually done is that a "shebang" line is added to the top of the shell script to explicitly define which shell will be used to run the script. So what the single line script above should really look like is this:
#!/bin/sh
python test.py
This may be starting to make your head spin. I would suggest reviewing the link I gave above, and possibly reviewing some other materials that explain shell scripts. Note that nothing about shell scripts is unique to the Mac. The concept is exactly the same on Linux, Unix, etc.
BTW, do you really want pip install pip? What does that do? Doesn't the pip package have to already be installed if the pip command is working?

How to run python programs in Windows 7?

I downloaded this tool to migrate MySQL to PostgreSQL: https://github.com/philipsoutham/py-mysql2pgsql
Python interactive code works properly so the python path is set in the right way.
When I type "py-mysql2pgsql" being in the directory: C:\Users\me, the downloaded tool doesn't run but ask me to choose the program to open that file. The same situation when I'm in C:\Users\me\py-mysql2pgsql
How can I run this tool properly?
Windows does not understand shebang lines in scripts (#!/usr/bin/env python) like Linux and Unix variants do. So Windows does not understand that this is a python script, you need to execute python yourself.
If python executable is in your path, you should be able to run:
python py-mysql2pgsql
If it is not in your path, you should be able to run:
path_to_python\python py-mysql2pgsql (on my machine C:\Python27\python)
Note that this applies to any python script on Windows, not just this tool.

Python 2.7 not loading in Cygwin

I'm having an issued getting my install of python27 to load correctly in Cygwin. My install has 2.6 (/bin/python# ) on it, but I'd like the default Python environment to be 2.7 (/cygdrive/c/Python27/python.exe). I know how to change my $PATH and all, but when I try to run this 2.7 installation through Cygwin by going to its directory and typing python it never loads; I have to end the command with CTRL-c. Is this a Cygwin problem, a Windows problem, or a me problem?
I think you can find some answers in this other question.
It is rather a problem of using the Windows version of Python with the Cygwin terminal.
Quick summary:
for interactive mode, add the -i option
if you want to see the output of your script, add the -u option.
I faced the same problem with Python 2.7 on Cygwin. Launching Python with -i option did get me to the interpreter, but it would not allow the use of the "up" cursor key to repeat older commands.
What you need to do is to launch the /bin/bash.exe from a DOS prompt. This will launch the standard Windows console instead the Cygwin console.
This answer sums it up pretty well.

How to run Python script on Ubuntu, Windows and MAC

I'm making Python software using wx GUI library but was wondering how to run this script on different OS's. For example, do I need to create executable installation file or bat file on Windows and sh file in Ubuntu?
I've got #!/usr/bin/env python at the top of the file and I can seem to run it by actually double clicking it and clicking it on "RUN" on prompt window but I would like it to be more professional as the users are not programmers.
Being used to run python scripts on both linux and Windows environments, I know that you can use the same script for both environments.
Keep using your shebang in Linux, it won't be procesed in windows (as it is actually a comment :).
Once Python is installed in Windows, you can actually simply double click on the script (it will run by default in a cmd window), run it using the cmd or launch it in idle.
If you want to develop python scripts on windows however, you'll need some more tools :).
If you want to be more professional (and prevent your users to modify the code :), you can still think about creating an exe file : http://www.lyxia.org/blog/developpement/python/creez-des-executables-46
(warning, french inside), by using pyinstaller http://www.pyinstaller.org/ . Works for windows and linux

Categories