I thought it might be time to start using PowerShell instead of cmd. I currently run Python scripts from Notepad++ with the following commnad:
cmd /c cd "$(CURRENT_DIRECTORY)" &python -i "$(FILE_NAME)"
How should I modify this to get the same behaviour in PowerShell please?
I tried
powershell -noexit cd "$(CURRENT_DIRECTORY)" &python -i "$(FILE_NAME)"
but I got an error about ampersands. I know very little about PowerShell or cmd in general - I just use them to run my Python scripts.
After a bit more digging I found a solution:
powershell -noexit cd '$(CURRENT_DIRECTORY)'; python -i '$(FILE_NAME)'
PowerShell seems to prefer single quotes for paths so I wrapped $(CURRENT_DIRECTORY) and $(FILE_NAME) in single quotes.
Also, & is replaced by ; for multiple commands.
If there is a more "correct" way of doing this, please let me know.
Related
I would like to run a Python script setting in the shell where the interpreter must look for the modules.
Suppose that myscript.py contains only:
import mymodule ; mymodule.myfunction()
But mymodule is in /home/user/hello, whereas myscript.py is, say, in /home/user/Desktop. I want to run on a terminal something like:
$ python /home/user/Desktop/myscript.py LOCATION_OF_THE_MODULES=/home/user/hello.
Would it be possible? I think that an alternative solution is to define the location in the import statement from the code, but this is not what I am looking for. I want to set the location through a variable in the shell.
So, I've been exploring a little your question, turns out this isn't a Python question but a "prompt" question, because indeed there is a way to do that but, since Python can't hop into interactive from script, we can't make it using Python only, but the Python interactive command have some extra options we can use
see:
python3 -h
for more info.
Specifically there are 2 options that are interesting, -i and -c which stands for interactive mode and command string respectively, that way we can load the modules with -c and hop into interactive with -i, like so:
python3 -i -c "import os"
Obviously, we need to make it more advanced so it can load multiple modules without Python scripting, then we will be needing to make the actual command to run Python and load the scripts you want, there is a problem tho, since we need to issue a command to be able to load all the modules you want in a folder it might create incompatibilities with prompts since not all prompts have the same syntax. There might be another low-level answer to this problem but I couldn't get to it, however, I will leave a Bash Script for reference so you can use it and/or edit it so it works best with your prompt.
FINAL_VAR=""
cd $1
for f in *.py; do
FINAL_VAR+="import ${f%.py}"$'\n'
done
python3 -i -c "$FINAL_VAR"
Usage steps:
Copy and save the script
Give it run permissions (chmod +x file_name.sh)
Run it this way: ./file_name.sh "/full/path/to/your/modules"
It will load all the .py files and will hop into an interactive Python shell for your use
Note: You might want to change the last line so it works accordingly to your Python installation
I would like to execute a circleCN.py python script by using #!/bin/sh.
I have searched the internet, found some suggestions, and none work. I get permission denied, and I changed my chmod of circleCN.py with a+x, I get invalid syntax, or I get another error. I have tried . /path/circleCN.py, exec /path/circleCN.py, python -c "/path/circleCN.py" and none work.
I have also tried to change #!/bin/sh to #!/usr/bin/env python and had no success. I would like to keep #!/bin/sh though.
I am very new to shell programming. I am also new to Linux.
When OpenFOAM users write Allrun or Allclean scripts, they use the #!/bin/sh shebang. I am working on an automation script and I would like to conform to the standard.
Why do you need to execute python script under a bash shebang?
You can invoke python script inside the shell script like this
#!/bin/sh
python /path_to_file/circleCN.py
I have a python script which I can run via PowerShell using the following code:
cd User\PythonScripts
python TestFile.py
Now I want to run these simple commands via a PowerShell script (notepad file and saving it as a ps1 file).
I have googled a lot but I cannot find an answer, but I think it should be something like this:
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py
I think I still miss the reference to python (so it knows which program he needs). How do I need to do this?
Assuming that python is already in your path variables you can just call a python script like this:
python C:\User\PythonScripts\TestFile.py
I think the question is you want to run the python script using powershell .
I think below code will do for you
$path = 'C:\User\PythonScripts'
$file = 'TestFile.py'
$cmd = $path+"\\"+$file # This line of code will create the concatenate the path and file
Start-Process $cmd # This line will execute the cmd
Save the above code as .ps1 and run the that powershell file
I have been facing the same problem with Windows10 PowerShell and I have solved it like this:
Go to PowerShell and instead of running the command like this:
PS C:\Windows\system32> python ex1.py
run it as below:
PS C:\Windows\system32> python C:\Users\PCWIZARD\Desktop\hardway\ex1.py
In other words specify the entire path of your ex1.py depending on where you saved your file on your computer. This will print out your code.
I think below code will work.
cd C:\User\PythonScripts
.\python TestFile.py
Mention python.exe if it is a executable in place of python.
Save above code in .ps1 file and run it on powershell
Can I use a batch file to start a python shell and execute commands? I know that
python
will start a python shell, but a batch file containing
python
1+1
will first run python, and then only when you quit python will it attempt to run 1+1. It will not execute any commands within the python shell.
After a little searching around, I managed to find this website that has a method to do this. As you will see on the website, all you need to do is:
#setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL!
#start python code here
print "hello world"
This didn't work for me, however I thought it might help.
I haven't been able to find any other source that says it's possible.
Just thought of something else that I haven't tested. I combined Bear's answer and mine.
#for /f "skip=1 delims=" %i in (%0) do #python -c "%i"
#Start Python here.
However, the other method should be used.
I know this has an accepted answer but you might also try the -c argument for the python command. python -c "print(1+1)" will print "2" to the console. The -c flag means "command" and is interpreted by python immediately.
I'm working on windows vista, but I'm running python from DOS command. I have this simple python program. (It's actually one py file named test.py)
import os
os.system('cd ..')
When I execute "python test.py" from a Dos command, it doesn't work.
For example, if the prompt Dos Command before execution was this:
C:\Directory>
After execution, must be this:
C:\>
Help Plz.
First, you generally don't want to use os.system - take a look at the subprocess module instead. But, that won't solve your immediate problem (just some you might have down the track) - the actual reason cd won't work is because it changes the working directory of the subprocess, and doesn't affect the process Python is running in - to do that, use os.chdir.
I don't really use Windows, but you can try cmd /k yourcommandhere. This executes the command and then returns to the CMD prompt.
So for example, maybe you can do what you want like this:
subprocess.call(['cmd', '/k', 'cd .. && prompt changed'])
As I said, I am not familiar with Windows, so the syntax could be wrong, but you should get the idea.
In case you don't know, this is a different CMD instance than the one you were in before you started your python script. So when you exit, your python script should continue execution, and after it's done, you'll be back to your original CMD.