I will try to be brief!
For whatever reason, I couldn't make the entirety of my program in python, so I had to outsource one specific task to php (a language I do not know very well). As the python program runs, it is supposed to trigger the php program to run, and then do a few things afterwards which is not a problem.
It seems to me that, to be able to run something through python, you need it to be able to run through cmd first, and then you can make python use cmd to run the program. I had a few issues there, because the programs are on different drives, and the php program references other files and locations in the same directory and in sub-directories to where it is, this means I couldn't execute in one line of cmd, but first had to change directory, to then execute the php program from the folder it's in. Because my command wasn't just one line, I made a batch file containing all the steps.
My current working method is to open up cmd, change directory in cmd to where the php file is, and then run the php file. I had to add php to the "Environment Variable Path" to be able to do this. Here is the batch file that currently works when run by me:
cd /d C:
cd C:\Users\UserMain\Desktop\php\colorextract
php (2).php
When I double click this bat file, from my E drive, it successfully executes the php program. But when I tell python to execute the batch file, that is where things go wrong.
Here is my python code, apologies for the name of the bat file:
import os
os.system('cmd /k "bitch.bat"')
The resultant cmd window then goes thru the steps of the batch file: 1) it changes to the right directory, 2) it is unable to execute the php file because:
'php' is not recognised as an internal or external command, operable program or batch file.
Now, this is the standard error you get if you were to try running a php program without having added php to the "Environment Variable Path", I know this because I went through that same thing. But if I manually open a cmd window, not administrative or anything, I can 1) successfully perform the steps outlined in batch file, and program runs, and 2) I can even run the bat file, and that also runs the program.
The cmd window opened by python does not seem to be able to reference the "Environment Variable Path", or it is for another reason somehow handicapped against being able to do all the things that a normal cmd widow can. How can this be fixed?
Thanks in advance to anyone who reads this!
Edit: I found that python had not detected the changes I made to the environment variables the day before, hence why python's cmd was giving the exact error that not having php in the environment variable gives. After I restarted my computer, my code worked. Thank you to #Gerhard and #Aaron Junker for making me think much harder about this issue.
so I found a command that can be run after importing os.
print(os.environ)
I ran this, and it told me that Python could not see that php had been added to the environment variables, well, more likely that python did not have the most up to date information regarding what was in the path variable(s).
Restarting my computer made the changes kick in, and now my original code works. Whilst I do feel very stupid, I'm just happy that this is resolved.
This seems to me like both instances use different environment variables.
Open
System Properties -> Advanced -> environment variables and look that PHP is in the PATH variable in user variables and in System variables.
Related
TL;DR I want to write a batch file that will do some stuff when a "No Python at " error is encountered
I develop and maintain several Python-based automation tools as my job and often, my users will encounter the dreaded "no Python at " error when the tool's virtual environment attempts to activate. I include a batch file that fixes the problem with all downloads of my tools, and it works like a charm, but they currently have to run it manually. I'd like to see if I can set things up so that the fix runs automatically when the error occurs.
Unfortunately, my skill at writing complex behavior in batch files is a bit lacking, and my Googling didn't turn up anything obvious for the tack I'm trying to perform.
Anyway, here's the batch file that runs the Python script:
"venv\Scripts\python.exe" Some_Python_Script.py
And here's what I want to run in the event of a "no Python at <path." error:
for /f "usebackq delims=#" %%a in (`where /r "C:\ProgramData\Microsoft\Windows\Start Menu\Programs" Python?3.??(64*`) do set pypath="%%a"
cd "%~dp0"
%pypath% "%~dp0\fixVenvConfig.py"
^^^The tools are run inside of a secure VM, and the geniuses who install Python on them for us never check the box to add Python to the system path, so I instead must locate a Python executable by following the shortcut in the start menu. Once I have a usable Python interpreter, I pass its file path into a Python script that then fixes the config file of the virtual environment used by the automation tools. I don't remember how half of the stuff in the above batch file script actually works. I just know that it does, so I just don't touch it.
In the end, I'm trying to figure out how to make it so that if what's in the first code block results in a "no Python at " error, the code in the second block gets run, and then the first block gets tried again. As previously mentioned, however, this is a bit beyond my level of batch file expertise. Please help?
I'm running windows
I've got a python script that works as expected when I run it from my interpreter (Anaconda, i think that's an interpreter...), but when I run it from my file manager by double clicking the script ("script.py") I see a quick black screen flash, but nothing else. I'm using the input() function, so that's not the issue.
When I try the same thing with a simple test script, which is just the print() function and the input function, the command line or terminal screen, whatever the black screen is called, displays my printed string and closes only after I press enter.
I believe my issue has to do with file paths and working directories, but I'm a noob and I don't know how to solve this. My script declares this variable:
inv_folder=r"C:\Users\domin\OneDrive\Desktop\Test Folder" #folder to look for completed recipe files
which I believe is an absolute filepath. I then loop through the files in that folder and open and read them
for filename in os.listdir(inv_folder):
Main goal is to send my script to someone else's computer, and allow them to run it simply by double clicking on the file. Trying to do that on my computer and failing
Doubleclicking the .py file in Windows will generally run the file. In your case, if all your script is doing is looking for a directory and looping through files in it, it is quite possible that the script is working correctly, but that it is closing immediately upon completing the script.
E.g. if you have the following python file hello.py with the single line of code.
print("hello world")
If you doubleclick hello.py, you will only see a black window flash for a second, as the program runs and exits.
To check whether your code is working correctly or not, you are best to navigate to that folder in your command prompt and run the program within the command prompt. E.g. typing "hello.py" in the right folder will then show the correct output in the console.
Try running it in the console first, seeing if the code is giving any errors, and then working from there. But the behaviour you are describing is basically standard Windows behaviour and not necessarily an error.
If you need to pause the window after running, please see the following StackOverflow question and answers about "Python Equivalent to System('PAUSE')"
Have you considered to compile it?
You can compile it easily with pyinstaller and this command:
C:\<path_to_python>\Scripts\pyinstaller.exe --onefile -n <your script name> <your script path>
Of course replace the <> with the relevant paths for you.
I am using python 3 on Win10 and running my code by opening a Command Prompt window and typing the file location. However, the window closes as soon as the program terminates, and before I can read any errors.
Edit: This happens whether or not the program has errors.
Thank you.
Solution 1:
I just saw your comment:
When I do that, I recieve the error 'python' is not recognized as an
internal or external command, operable program or batch file
It looks like you haven't specified the path to the python executable: you need to add the python executable path to your Window's PATH variable. You can see how to do that here: Add Python to the PATH Environmental Variable (‘python’ is not recognized as an internal or external command)
Solution 2:
You can use input("enter to exit") at the end of your python code to keep the program alive. It would exit once you press enter.
You could also surround your code in a try except statement and place thr input() in the except to prevent the program from exiting when there are errors, but like #Kevin mentioned in the comments, this would catch run time errors but not syntax errors.
Solution 3:
You can write errors or anything information you want to a file such as log.txt for example, and then read that log file once the code finishes running e.g. how to write to a file in Python
FWIW, I have several Python versions on my Windows system, so I don't want to add any Python directories to my path permanently.
Code for each version is in a separate folder (e.g. 'py37'), with a subfolder for each project e.g. 'myProject'.
In py37, there's a batch file called pyEnv.bat with this content:
#echo off
path=%path%;C:\Python37\;C:\Python37\Scripts\
cd.
cmd
In Windows explorer, I head over to the project folder I want to work in, click in the address bar and type ..\pyEnv. That launches a DOS-box, in which I now can do python myproject.py. You can see print() output, errors, and so on.
You can up-arrow to try different modules, having typed them first.
Once you quit the DOS-box, your path is back to normal again.
I am very new to Python and I have been trying to find a way to write in cmd with python.
I tried os.system and subprocess too. But I am not sure how to use subprocess.
While using os.system(), I got an error saying that the file specified cannot be found.
This is what I am trying to write in cmd os.system('cd '+path+'tesseract '+'a.png out')
I have tried searching Google but still I don't understand how to use subprocess.
EDIT:
It's not a problem with python anymore, I have figured out. Here is my code now.
os.system("cd C:\\Users\\User\\Desktop\\Folder\\data\\")
os.system("tesseract a.png out")
Now it says the file cannot be open. But if I open the cmd separately and write the above code, it successfully creates a file in the folder\data.
Each call to os.system is a separate instance of the shell. The cd you issued only had effect in the first instance of the shell. The second call to os.system was a new shell instance that started in the Python program's current working directory, which was not affected by the first cd invocation.
Some ways to do what you want:
1 -- put all the relevant commands in a single bash file and execute that via os.system
2 -- skip the cd call; just invoke your tesseract command using a full path to the file
3 -- change the directory for the Python program as a whole using os.chdir but this is probably not the right way -- your Python program as a whole (especially if running in a web app framework like Django or web2py) may have strong feelings about the current working directory.
The main takeaway is, os.system calls don't change the execution environment of the current Python program. It's equivalent to what would happen if you created a sub-shell at the command line, issued one command then exited. Some commands (like creating files or directories) have permanent effect. Others (like changing directories or setting environment variables) don't.
EDIT: I got it working, I went into the pycassa directory and typed python pycassaShell but the 2nd part of my question (at the bottom there) is still valid: how do I run a script in pycassaShell?
I recently installed Cassandra and pycassa and followed the instruction from here.
They work fine, except I cant get pycassaShell to load. When I type pycassaShell at the command prompt, I get
'pycassaShell' is not recognized as an internal or external command,
operable program or batch file.
Do I need to set up a path for it?
Also, does anyone know if you can run ddl scripts using pycassaShell? It is for this reason that I want to try it out. At the moment, I'm doing all my ddl in the cassandra CLI, I'd like to be able to put it in a script to automate it.
You probably don't want to be running scripts with pycassaShell. It's designed more as an interactive environment to quickly try things out. For serious scripts, I recommend just writing a normal python script that imports pycassa and sets up the connection pool and column families itself; it should only be an extra 5 or so lines.
However, there is an (undocumented, I just noticed) optional -f or --file flag that you can use. It will essentially run execfile() on that script after startup completes, so you can use the SYSTEM_MANAGER and CF variables that are already set up in your script. This is intended primarily to be used as a prep script for your environment, similar to how you might use a .bashrc file (I don't know of a Windows equivalent).
Regarding DDL statements, I suggest you look at the SystemManager class.