Running a python script via Powershell script - python

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

Related

A python problem in running the command to execute a python file in vs code

I was trying to run a python file "exe.py" on vs code by typing:
python3 .\exe.py
But it comes with the following replys from vs code:
python3: can't open file '/Users/exercise/my-python-app/myenv/.exe.py': [Errno 2] No such file or directory
No ideas why couldn't it works given that I already installed the python package and running the command in appropriate environment.
You are running
python3 .\exe.py
which is escaping the e and therefore is replaced by e as there's no matching escape char and it becomes
python3 .exe.py
You need, as other response states
python3 exe.py
or
python3 ./exe.py
In fact python3 is installed in your system but you are typing incorrectly the command. Imagine that you have the python file named exe.py with the following code
print("Hello world")
If you are in the same directory of the file you can execute it using the command
python3 exe.py
Another way to execute exe.py is using the shebang line. The shebang is a line that you can write to tell to the operative system which executable must use to interpret the code of the file. If you add the typical shebang for python3 in linux at the beginning of exe.py
#!/usr/bin/env python3
print("Hello world")
and you tip the command
./exe.py
you will see the result of the execution too. If you get an error probably you have to give permissions to the file with the command
chmod 777 exe.py
You can find a lot of resources talking about the shebang. The following link talks about the shebang in python
https://www.pythonpool.com/python-shebang/
I hope you found it useful.
Greetings.

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])

Run Python code without using python name.py and ./name [duplicate]

This question already has answers here:
how to run python script without typing 'python ...'
(5 answers)
Closed 2 years ago.
I have a basic problem where I don't know how to run a Python script from command line in Ubuntu without using python keyword. So, I put a shebang in my Python script so I could run it as nameofthescript from the command line, but I only could do it by using ./nameofthescript. I want to be able to run it by just typing the name of the script in the cmd. I searched and tried everything I could on the web, but none is working. Any help is appreciated. Below is a simple code I wrote to test it.
I already tried chmod +x this file. Also this file is saved with no extension.
#!/usr/bin/python
import sys
def main(argv):
print(argv)
print("Hello")
if __name__ == "__main__":
main(sys.argv[1:])
The problem is with your $PATH variable.
When you go to run a command (without the "./" in front of it) Ubuntu looks in all the folders listed in your $PATH variable. Your can see it by running:
echo $PATH
If Ubuntu doesn't see the command in any of those folders, it will say that it can't be found.
You can solve this problem by altering your $PATH variable in your profile. Go to your home directory and open the ".profile" file (note the period in front) and add the following to the end:
PATH = "/path/to/folder/with/file/:$PATH"
However, if it's a program you could see yourself using a lot in the future and your don't want to clutter up your $PATH, I'd recommend sticking the finished command in your "/usr/local/bin" folder instead. I find that folder gets used as an "odd sock drawer" of programs you create/compile yourself, so I usually end up putting my personal tools in there rather than modifying my $PATH.
That's how it's supposed to work, not only for Python scripts but for any executable. See: Why do you need ./ (dot-slash) before executable or script name to run it in bash?
Please Try this one
On unix systems, Python scripts can be made executable using the following process:
Add this line as the first line in the script:
#!/usr/bin/env python
At the unix command prompt, type the following to make myexe.py executable:
$ chmod +x myexe.py
Move myexe.py into your bin directory, and it will be runnable from anywhere.
$ cp myexe.py /usr/bin
OR
$ cp myexe.py /usr/local/bin
So myexe.py
#!/usr/bin/env python
print("Hello This is executable python script")
Now Go to terminal and type myexe.py
$ myexe.py
Hello This is excutable python script
If you want to run by double-clicking remove .py extention
source link
I have found a way to solve this. I still include the shebang #!/usr/bin/env python3.6 at the top of Python script. Then I would go to cd /etc->sudo nano bash.bashrc, and at the very last line, all I did was add a line (alias nameofscript = "./nameofscript"). From there I restarted my Ubuntu, and was able to run my Python script just by the name of the script. Thank you everyone for the help.

Need a solution to execute python code, ".py", in the sh environment

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

How do I call a Python/Perl script in bin folder from a Bash script?

I previously used to copy Python/Perl scripts to access from my bash script. Duplication is not a good idea I know! Is there a way to call them from bin or libs folder that we have set up?
For instance :
My python script resides in /home/ThinkCode/libs/python/script.py
My bash script resides in /home/ThinkCode/NewProcess/ProjectA/run.sh
Now I want to use/call script.py from run.sh
Thank you!
Make this the first line of your python script (bash will then know this is a python script and it should be run with python):
#/usr/bin/env python
EDIT: my bad, it should be #!/usr/bin/env python not #!/usr/bin/python. It is better to do it this way.
Then chmod your script with u+x (if not a+x).
Now your python script works as an executable. Your bash script, then, can call it like you'd call any executable.
Just do python /path/to/my/python/script.py.
In a bash script, you execute programs the same way you do from the bash command prompt.
/home/ThinkCode/libs/python/script.py
If this doesn't launch the script directly, you may need to add python to the beginning (like this: python /home/ThinkCode/libs/python/script.py) and/or ensure that the script is executable (with chmod +x /home/ThinkCode/libs/python/script.py).

Categories