So for a little context. In linux the "ifconfig" command is actually executing a "ifcfg-eth0" file found "/etc/sysconfig/network-scripts."
In windows, do command line (or powershell) commands correspond to a specific file? If so where? If it exists I having a hard time finding it.
Reason:
I am trying to execute commands from a program I am writing in Python. I know there are other ways to accomplish this ie. "import os, import subproccess." I am trying to brainstorm a simpler way to execute these commands before my program gets to heavy.
Basically I would like to tell python to execute a file ie. "ifcfg-eth0" in linux but in windows. Also, I'm just using "ipconfig" as an example There are a lot of commands I want to add.
In Windows, an easy way to find the path of a program is to use Where.exe.
where.exe ipconfig
Usually cmd commands are located in system32.
There's an executable ipconfig.exe in C:\Windows\system32
Related
Hello I am having this problem where i want to automatically check the system32 file folder with the sfc cmd commands like sfc /verifyonly but the problem is that I can execute only one line in the cmd but I need to execute two and don't know how to execute a second one
import os
os.system("start /B start cmd.exe #cmd /k sfc")
I need another command for sfc /verifyonly so the program would work fully automatic can somebody help with anything pls
I tried a lot of things but nothing seemed to work for me or i am just really stupid and can't find the exact command i should be using
If you look at the docs for os.system they mention the subprocess module as being a more recent development within python that supports multiple systems level processes
Good afternoon,
I have been stuck with this problem for a couple of days now, and can't seem to get out of it.
Before running a model I have to generate the data in a certain format. To do so I have to run a .sh file, in which there are some calls to python. If I run the command "python" inside the power shell(my machine has Windows 10 as the Operating System), it works just fine. I have infact addedd python to the PATH variable of my system variables.
If instead I try to run the .sh file, in which I have calls to python, I get the following behaviour:
PS C:\Users\....\scripts>bash FILE_NAME.sh SRC_FILE.csv
data_mr exist
Start to converting data.
FILE_NAME.sh: line 48: python: command not found
It seems that when I try to run python inside a file, he cannot recognize/retrieve it from the system variables, which seems very odd to me. The bash file I am trying to run is provided by https://github.com/mindspore-ai/models/tree/master/official/gnn/bgcf , hence I doubt the problem is in the file.
Do you have any idea of where the problem could be and how to possibly solve it?
As far as I know, Windows does not have a shell which can handle .sh files. I believe you have a so-called WSL (Windows Subsystem for Linux), which means that you have a Linux/UNIX-system in top of your Windows.
This system has its own environment variables (check env | grep "PATH"). I would advise you to find the location of Python and add it to that specific PATH variable, but be aware that your Windows Python might not run on that Linux/UNIX system.
Oh, now that I think about it: are you talking about bash or GIT-bash?
In a GUI app written in Python and Tkinter, I want to add a menu command which will open the source code of the *.py file in IDLE. I also want it to be platform independent.
I've tried using os.system to open IDLE from Scripts folder of Python, but that would be platform dependent. I couldn't find a way to get the Scripts folder of Python to make it independent. How can I achieve this?
To open a file in an IDLE editor, the command line is
<python> -m idlelib <path>
where <python> is the full path to a python executable or a name that resolves to such. If you want to open IDLE with the same python that is running you python app, which is likely what you want, use the platform-independent sys.executable. The path to the source code for the running app is __file__. This is one of the semi-hidden global variables when python runs a file. Or give the path to any other file.
Whether you use os.system or subprocess is a different issue. The latter is more flexible. subprocess.run replaces os.system. subprocess.Popen should not block. IDLE uses the latter to run user code.
I tried some special way:
import idlelib.pyshell
import sys
sys.argv.clear()
sys.argv.append("")
sys.argv.append(fName)
idlelib.pyshell.main()
It works! But I don't know whether this way will generate some problems.
Background:
I'm using Windows. I know some of programming with python. I don't know much about batch, which I think I might need to do what I want.
I will show a example so it becomes more clear what I'm trying to do.
Example:
When using git, after you install it, you can call the git command from anywhere of your computer, you can execute git commands, like git init and this will create a git file in your current folder.
I don't know exactly how git works or what language they use but I want to do the same thing, create my own command that I can execute from anywhere in my computer after I "install" my program.
What I'm trying to do:
I want to make my own command and when I call it, it executes a python script.
e.g.
I install my program and it creates a command called myprogram and when I type myprogram in the command line, it's like if I typed python myprogram.py. And myprogram -someargument would be the same as python myprogram.py -someargument.
What I tried until now:
I'm searched for How to make a environment variable that runs Python script? but I never get exactly what I want, is always something like How do I set environment variable using Python script?.
Maybe I'm making the wrong question and the result I want are not showing?
I'm looking for a explanation on how to do this or at least a tutorial/guide.
Edit 1:
As UnholySheep said in the comments, it's not environment variable, its commands, so I changed the question even does what I want to know is the same thing.
Files you need:
First you need a python script (obviously) so I created a file called myprogram.py that have this simple line:
print("This should be from a command")
After you need to make a batch file, in my case I used a .cmd file called myprogram.cmd that have:
#ECHO OFF
python_directory\python.exe python_script_directory\myprogram.py %*
Configurations to make:
You need to set in PATH environment variable the location of the batch file batch_file_directory\myprogram.cmd
And now if you execute in the command line myprogram it will print This should be from a command.
You can also use .exe or .bat files.
I would like to use a python script anywhere within command prompt. This is possible in unix, but I couldn't find anything for windows. Do I have to convert it to .exe?
Edit: not sure why this is being downvoted, maybe it's a silly question but I can't find any similar threads here and I can't be the first person to want to execute .py scripts from their path...
Edit 2: I think my wording was unclear. I would like to know a method to execute python scripts in Windows without needing to specify python path/to/script.py every time. Here is a solution on Linux where the shebang statement invokes the python interpreter, and the script in question can be easily placed in bin: How do I install a script to run anywhere from the command line? . Does there exist a solution like this for Windows?
Here's a solution for running myScript.py:
add to the myScript.py file a first line #!python (or #!python3 if you want to use Python 3)
For instance:
#!python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
change the "opens with" property of myScript.py to py.exe (to find where it is use where py-- I have it in C:\Windows\py.exe)
put the script myScript.py somewhere in your Windows path
and now you should be able to type myScript.py anywhere in a command prompt and run the Python script with your chosen Python version.
See: https://docs.python.org/3.6/using/windows.html#from-the-command-line