Python script is not executed from my batch file - python

I have written the following code within a .bat file, (to execute a python script):
conda activate
cd OneDrive - My path/Documenti/Python/Seguridad/Remote
py Seguridad_Python.py
If I execute that command in cmd.exe, the python file is executed.
But, if I run the .bat file with those commands, it opens the Command Prompt window, but does not execute the python script.
Can someone help?
I have also tried it with the next one, (everything in the same line), and it does the same.
C:\ProgramData\Anaconda3\python.exe C:\Users\------ MY PATH --------\\Documenti\Python\Seguridad\Remote\Seguridad_Python.python

Adding a call before was enough.
If I have the batch file and the python script at the same level, this was enough:
call conda activate
call py Seguridad_Python.py

Maybe you can try as below. Specify the batch file extension in the command prompt. Once you hit enter, it invokes your python script and run the program
C:\Users\------ MY PATH --------\\Documenti\Python\Seguridad\Remote\xyz.bat

Related

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'])

Why is the following .sh script saying .py command not found when the script is in PATH?

I am trying to run the Example Workflow in https://rki_bioinformatics.gitlab.io/ditasic/, in which example.sh is the major bash script that will take the example data and output some data matrices.
In the example.sh script which will run the example workflow, we have the following line 9:
ditasic_matrix.py -l 100 -o output/similarity_matrix35.npy data/reference_paths
However, when example.sh is run in the terminal of macOS, the following message arises:
DiTaSic /ditasic_example/example.sh: line 9: ditasic_matrix.py: command not found
But ditasic_matrix.py already exists in the path I have set for the terminal. I have put ditasic_matrix.py in a directory whose path I have added to the PATH of the terminal by
export PATH="$PATH":
So what has happened that leads to the command not found?
Change ditasic_matrix.py line in your script to be ./ditasic_matrix.py because of current path not being included in executable search.
If it still doesn't execute, maybe the file does not have executable bit set.
Open a terminal/console in that folder and issue
chmod +x ditasic_matrix.py
The ditasic_matrix.py file seems to have the following interpreter setting on it: #!/usr/bin/env python. Since you seem to not be able to run it, it seems that this is not your actual path to running Python. Please make sure that:
1) You have Python installed
2) You can execute Python programs by running python in the command line.

Batch file is not working

#echo off
start c:\Python27\python.exe C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py
PAUSE
My script in tool.py is correctly working in PyCharm IDE, this bat is not working.
Note : file path and python path is correct.
Any other option to run python script independently
Try removing the start from your batch file - this opens a separate window for the console application python.exe, and when python.exe exits, it will close the console window. By eliminating start, it runs python.exe in the same session of cmd that the batch file is running in, and allows you to read any output from python.exe before exiting (because of the pause command).
Python installations by default associate .py files with it and issues a command to run the interpreter PATH_TO_PYTHON\python.exe "%1" %*
So you should not need to call python.exe manually to call the script. You also do not need the start command at all.
Not sure why you need the batch to call a python script, but this would be perfect. always put the full path in quotes in case you hit whitespace in the paths.
#echo off
"C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py"
pause

Executing a Python script from the command line in Windows 7

I seem to be having a problem with executing a Python script from a Win+R terminal.
I completed the following steps:
Used the shebang line before typing my script for all .py files. An example of what I did below for a script called Primefactorization.py.
#! python3
I created a batch file in the same folder with the same name and input the following code:
#python.exe C:\Python Scripts\Primefactorization.py %*
I added the path (C:\Python Scripts) to the PATH variable in the environment variables window.
When I try to invoke the script using the Run command in Windows 7, the shell opens and immediately disappears.
Based on a past answer to a similar problem on Stack Overflow, I also added the following code prompting the user for input before exiting. But that doesn't seem to work.
x = input('press enter to close')
Could you please let me know where the problem might be?
Your path has a space in it. Enclose it in double quotes. For example:
python.exe "C:\Python Scripts\Primefactorization.py"

Run python script in terminal

I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.

Categories