We all know that windows has the feature that you can right click on a file and numerous options are shown. Well you can add a value to this menu. I followed this guide : jfitz.com/tips/rclick_custom.html
Basically I have a script that runs when I right click on a certain file type.
Alright, so everything is going flawlessly, however when I finish and try to test it out Windows tells me that it only accepts .exe's.
Is their a way around this without having to use py2exe or something similar?
**EDIT
Ok , so if my script takes the first argument as it's parameter how would I put this into the registry?
"C:\Python26\pythonw.exe C:\Users\daved\Documents\Python\backup.py "%1""
?
yes, call pythonw.exe and pass the script path as a parameter
"C:\Python26\pythonw.exe" "C:\Users\daved\Documents\Python\backup.py" "%1"
It's also recommended (but not required) to use the extension .pyw when your script doesn't run in a console.
Related
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.
I'm running a script that involves a lot of foreign characters, and the classic cmd line seems to choke on them, which might be related to my Windows setup. It runs fine in IDLE and other IDE's. Is there anyway that I can launch my script automatically from another place (e.g. IDLE) without first having to open another program?
You can just make a shortcut of the form:
"C:\Python27\python.exe" "C:\location\of\your\script.py"
Obviously substitute your version of Python. If you include arguments that also point to a file that contains your foreign characters, you can have it load that as well.
To have the command windows stay open, use one of the following at the end of your script:
# Python 2
raw_input()
# Python 3
input()
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.
I decided to use NppExec to run my Python files from notepad++, however I encountered a problem. I created a simple script with one line:
python -i $(FULL_CURRENT_PATH)
and added the script to the "Menu items" in nppExec advanced options. The script appears in my macros tab and does what I need when I run it. The problem is I am unable to set a keyboard shortcut to this command. The command appears in my shortcut mapper (under plugin commands) and I can set it to
shift+ctrl+p
but the problem is that it simply does not work when I hit the three keys. The key combination is not the problem, as I was able to create a test macro that ran when I hit shift+ctrl+p.
Edit:
More updates: the shortcuts.xml in APPDATA does change when I add the shortcut, the line:
<PluginCommand moduleName="NppExec.dll" internalID="20" Ctrl="yes" Alt="no" Shift="yes" Key="80" />
is added. This is exactly my ctrl+shift+p command, it's there, it just does not fire.
Is anyone else experiencing this strange behaviour?
Any help will be greatly appretiated.
Ctrl+Shift+P is by default assigned to Playback command. Notepad++ is buggy when it comes to shortcut mapping, can easily create collisions. See for example open bug #3635.
My suggestion is to try to assign it to something else (personal preference is F5) and double check that it's not already assigned!
I'm new to python and pycharm and I'd like to run a module from the pycharm console in the same way as you can from IDLE, if it's possible.
The idea is to create simple functions and test them "live" using the console.
...how do you do that in pycharm?
Running python scripts using pycharm is pretty straightforward, quote from docs:
To run a script with a temporary run/debug configuration Open the
desired script in the editor, or select it in the Project tool window.
Choose Run on the context menu, or press Ctrl+Shift+F10. So
doing, a temporary run/debug configuration is created on-the-fly.
Besides there is a "Python Console" available in pycharm: see documentation.
UPD:
Here's an example.
Imagine you have a python module called test_module.py:
def a(*args, **kwargs):
print "I'm function a"
def b(*args, **kwargs):
print "I'm function b"
Then, in pycharm's "Python Console" you can do this:
>>> from test_module import *
>>> a()
I'm function a
>>> b()
I'm function b
If you need to execute a part of an existing code, you can use the Execute Selection in Console feature: select the code snippet -> right click -> "Execute Selection in Console".
For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.
Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)
Right Click --> Run File In Console
Done!
Looks like in version 2018.3, this option is now Run with Python console in Run/Debug Configurations:
What you're looking for is the feature called Execute Selection in Console which is described in section Loading Code from Editor Into Console of PyCharm's online help.
Select the script lines that you want to execute and press Shift+Alt+E
You can run the Find Action shortcut (Ctrl+Shift+A or ⌘+⇧+A on mac), then type run file, and choose the option Run file in Console.
In pycharm do:
Run>Edit Configuration>Show command line afterwards
Assuming your code is in file MySimpleCode.py you can simply say
run MySimpleCode
in the PyCharm console. This assumes that you have set your working directory properly; e.g. if MySimpleCode.py is located in d:\work on a Windows system you must execute
cd d:\work
first. In my opinion the other solutions miss what the post really wants: simply executing a file like from a DOS or Unix shell, or a .m script in MATLAB. No messing with imports, projects and so on. If you use CTRL SHIFT F10 your code gets executed, sure, but in a different environment, so you have no access to variables created in your code. I assume the question means that you want to do further work with the results of the script.
Explanation for people with MATLAB background: In most Python IDEs you have to configure an interpreter first in some kind of project. The MATLAB equivalent would be a master IDE where you can choose your MATLAB version for each project. This makes it possible to run your Python code on the CPU, your GPU or even an external NVIDIA board with different settings (after several days in the installation hell). For the beginner this is very confusing, because for simple code samples any "default" interpreter should suffice. Unfortunately this is not the case for Python (2 or 3? 2.x or 2.y? which package version?), and it will get worse as you progress (which 32 or 64 bit version of TensorFlow is available for Python 3.x? and so on).