This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.
Related
This question already has answers here:
How do I make a python script executable?
(6 answers)
Closed last year.
Say I have the program program.py in a non-root directory. Thus, in order to run the program, I have to type python3 program.py every time. Is there a shortcut that I can enable in my unix environment where I type a specific command for a specific program to run. I.e. for the above, I want to just be able to type program and have the program run.
I am not greatly familiar with unix but I believe this has something to do with adding something to your path which I am unfamiliar with this method. Any references and/or support is greatly appreciated!!
Thanks!
Add this to the top of your file.
#!/usr/bin/python3, or wherever your install is.
Then chmod +x your_file.py.
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.
This question already has an answer here:
Python script start on boot
(1 answer)
Closed 3 years ago.
I want to monitor a file in a specific location and check it for updates, which upon being updated launches certain actions via a python script.
Ideally, this program would always be running in the background and would automatically start with the computer so that the user does not have to mess with starting/stopping it.
I have researched the topic some and have found daemon and similar tools, but I do not understand how they work and they look far more complex then what I need. Also, many of the examples that I am looking at use Ubuntu OS and I will be using Windows.
Are there any python modules that exist to do this already or any direction you can point me to get started? I apologize if this question has been answered already, however I have not found it in my research.
I plan on editing this post to include code that performs this action, however I currently do not know where to start.
On Windows you could create a batch script and schedule it using the Windows Task Scheduler to run as a process on boot.
You can trigger the Python script to run inside the batch file,
python file.py
Alternatively, if you're using something like Anaconda as your environment manager; you could write a batch file to activate Anaconda using the activate.bat contained in the Scripts folder of your Anaconda installation path and then follow the usual calling steps.
This question already has answers here:
Running Python code in Vim
(25 answers)
Closed 5 years ago.
I'm newbie in this editor but it's complex to configure vimrc. I know and understand that everything stay there but I don't understand how to configure, specifically the code. On the other hand, I am writing Python code using Vim. Every time I want to run my code, I type this inside Vim
:w !python
This gets frustrating, so I was looking for a quicker method to run Python code inside Vim. If somebody can I help me, I would be very grateful.
You've very close. The following works in my config:
nnoremap <F9> :w<cr>:!python %<cr>
First, it enters the command to save the file (:w). Then it tells the shell to have python (!python) run the current file (%).
EDIT: The code I wrote in my Python file was just this:
print "foo"
I'm using Windows XP Home Premium on this tiny little HP Mini 1000, and I want to run Python files, since we're learning it in school. I am aware of this topic, so I tried to run Python files using a batch file (python.bat), and I'm getting an error that says, "Can't find 'main' module in ''" whenever I run the batch file. I followed the instructions given here. All I did was change "Python26" to "Python33" because of the difference in versions.
Any idea what's wrong here? I really want to run Python files from Notepad++, so I don't want any alternative ways to run them.
This sounds like you don't have PYTHONPATH set up correctly. I suggest you review the documentation here:
http://docs.python.org/2/using/windows.html
Instead of calling Python, call cmd.exe and then use the set command to inspect which variables are set and how they are set. Run the exit command to leave the command shell. When you think you have the variables set up correctly, try again to run Python.
Good luck and have fun!
I use the command line interpreter or IDLE mostly (Win 8.1 now, but I've done so since Win XP SP2), but NPP is my main text editor, so I was curious about this issue.
When I was reproducing this, I was able to generate several errors, but the only one I got that was an exact match was when I failed to configure the Run option correctly.
You need to make sure to follow this step exactly in the instructions you were following. When you navigate to Run -> Run in Notepad++, you have to enter this exactly:
C:\Python33\python.bat "$(FULL_CURRENT_PATH)"
I am pretty sure you left out the "$(FULL_CURRENT_PATH)", or otherwise didn't add it correctly, as failing to do so causes exactly the same error on my end. Failing to include this means that when you run the batch script, you get the wrong input to the Python interpreter, causing the error.