Notepad++ cannot find filepath - python

I'm trying to run a Hello World program in Python I wrote in Notepad++ using the NppExec plugin, but instead of printing, I'm getting
python C:\Users\Sam\Desktop\Test.py
CreateProcess() failed with error code 2:
The system cannot find the file specified.
The argument I'm giving NppExec is
python C:\Users\Sam\Desktop\Test.py
which is the filepath that NP++ gives me when I copy the full filepath to the clipboard.
Is there some configuration of NP++ that I have to set to get this to work?

I tried what you are attempting to do, and this is how I solved it:
Instead of passing the argument you gave, I passed this one:
C:\Python32\python.exe C:\Users\Sam\Desktop\Test.py for python 3+
C:\Python27\python.exe C:\Users\Sam\Desktop\Test.py for python 2
Generally, in order for it to work, you have to define where you have installed the python executable.

In general, you can use the following as an argument to NppExec for any currently opened Python script in Notepad++:
[Your Python install folder here]\python.exe "$(FULL_CURRENT_PATH)"
Note that "FULL_CURRENT_PATH" is a Notepad++ internal variable, not a placeholder for your file's actual path and filename, so the above argument should work without edits regardless of your current script's filename.
Further references for using NppExec with other source code: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Compiling_Source_Code

This is an alternative method to running python programs in notepad++, which I recommend after being unable to find a suitable plugin.
create a batch file called pythonXX.bat ( where XX is the current version of python you're using ) and save it along side your python.exe in C:\PythonXX\
and insert this text into that batch file:
#ECHO OFF
C:\PythonXX\python.exe "%1"
PAUSE
#ECHO ON
Then inside notepad++ create a run command:
C:\PythonXX\pythonXX.bat "$(FULL_CURRENT_PATH)"
Then click save run and assign it to a keyboard short-cut, good to go :)

Related

Run Python on Notepad++, execute screen

https://medium.com/never-too-late-to-machine-learn/how-to-step-by-step-setup-notepad-to-run-python-scripts-e1ce3b3ac7fe
I am reading from this tutorial linked above.
What I am doing is trying to run python for the first time on Notepad++. I've downloaded python, etc.
What I am lost on is this:
"Just copy the location of the python exe file, and let’s go back to Notepad++. The execute screen is waiting for us.
In the execute screen, paste the location of Python followed with “$(FILE_NAME)” "
What is the execute screen, where am I pasting the location to?
I hope someone can help me. Thank you.
first, you need "$(FULL_CURRENT_PATH)" for full path of file, the $(FILE_NAME) is for file name only or relative path
then you can paste like
C:\Python39\python.exe "$(FULL_CURRENT_PATH)"
for execute screen, new name for the menu is Execute NppExec Script.. see image below
to run your python script in notepad++ is quite simple:
make sure your python is correctly installed, open your console and type python, you should see something similar to the following (it should say Python X.Y.Z accordingly to the version you have installed)
now that we know that python is correctly installed, go to notepad++, open on it your desire script and go to the Run menu and select Run...
enter the following command python -i "$(FULL_CURRENT_PATH)" and press Run
You should see the following
And that is all.
-
Now that the previous worked, lets make it more reusable, repeat step 1 and 2, but instead of Run press Save..., give it a name (like python for example) and if you so desire also a keyboard binding (a key combination to press to run this command)
Now in step 1, you can pick python (or whatever you name it) instead or "Run..." to run you script (or press you key combination if you give it one)
now some additional explanation about the previous command python -i "$(FULL_CURRENT_PATH)"
the -i option is to enter in interactive mode, that is run you script and then continue executing python so you can do more python stuff with everything for your script loaded in there too, that way the console doesn't just close intermediately after your script is done running.
$(FULL_CURRENT_PATH) is a command for notepad++ to said to substitute there for the full path of your given script
Alternative command: cmd /K CD "$(CURRENT_DIRECTORY)" & python -i "$(FILE_NAME)"
This command is the equivalent to opening your console (cmd /K), move with cd to the folder where your script is (CD "$(CURRENT_DIRECTORY)") and then run your script with python in interactive mode (python -i "$(FILE_NAME)") and & is to separate both instructions (the /K is like python's -i but for the console) (the "-i" is now optional, but I prefer it with it to test other stuff in the script without need to put it on the script because doing so make little sense...)
Why you might want to use this over the other? two reason:
the first is when dealing files so you don't have to type the full path of a given file that sit next to your script so you can use just the name of said file, aka set the working directory to that where the script is located, otherwise it will be that where notepad++ is located (usually it might be like "C:\Program Files\Notepad++").
In case of an error, the windows will remain open so you can know what was the error instead of closing abruptly.

Running Python file by double-click

I have really annoying problem, I cannot run a Python file just by double-clicking.
I have tried to set it to open the file with idle.bat but that only starts IDLE editor on double-click, it does not run the Python file.
What version of Python do you have installed?
You should write your own batch file to execute your python binary and your script.
For example, with a default Python 2.7 installation on Windows, this could be the entire contents of your script.
myscript.bat:
ECHO ON
REM A batch script to execute a Python script
SET PATH=%PATH%;C:\Python27
python yourscript.py
PAUSE
Save this file as "myscript.bat" (make sure it's not "myscript.bat.txt"), then double click it.
right click on the file->open with->choose default program->more options->select python.exe file and click on.
Right click the file, select open with. If you want to simply run the script, find python.exe and select it. If you want to debug with IDLE, find that executable and select it.
When I had both Py2 and Py3, and then removed the former, my script wouldn't run by double-clicking it either (but fine from console.) I realized my __pycache__ folder (same directory as the script) was the issue. Problem solved when deleted.
You can also start a Django app this way. Once the Django server starts it enters a "wait" kind of mode so a batch file only requires two lines:
ECHO ON
python manage.py runserver
Manage.py can be in any directory, just keep the full folder path in the command within the batch file:
ECHO ON
python C:\temp\manage.py runserver
Solution for Ubuntu users.
Right-click on the file, then select open with, then choose your python version, it shode be in
/bin folder, usually it's /bin/python3.exe
In Windows 10, using regedit:
Under HKEY_CLASSES_ROOT, create key: .py
Edit Default string value to be py_auto_file (or any other name you want to call it)
Under HKCR, create another key: py_auto_file (or any matching name that you have just picked)
Under this key, create nested sub-keys: shell --> open --> command.
Edit Default string value to "C:\path\to\your\python.exe" "%1"

how to output all the lines into python console in vim?

I have set F2 prompt key with map <f2> :w<cr>:! D:\Python34\python %<cr>,when i open an python file in vim and press F2,the python file will be executed .For a simple example,
here is my python file and opened in gvim .
Now i can't input other python lines ,only thing i can do is to see the result and hit any key to close this window.
What i want is :
when i press F2, (the python file was opened in gvim) ,the python console pop up,and all the files in the python file were copied into the python console automatically,and i can go no to input some lines such as Obj().hello in the python console or go on to edit in gvim ,i am a lazy man ,the gvim and python console all opened waiting to serve me , can i write a vim scripts to achieve the target?
The command :!D:\Python34\python -i % works fine ,i got the ouput
There is still a problem remain,
1)when command :!D:\Python34\python -i % works ,the gvim window will be frozen , i can't drag my mouse to see codes in vim.
2)there is no any python codes in the python console wiondow
So if the program is full of many lines ,and i can't remember the previous content ,worse still, the gvim window frozen ,how can i get the codes?
Avoid blocking
To make the call asynchonous (to avoid that GVIM is blocked during the Python session), use the Windows-specific :!start command:
nnoremap <f2> :w<cr>:!start D:\Python34\python -i %<cr>
List teh codez
I don't know whether it is possible to list the passed source code from the interactive Python debugger. But you can print the file contents before starting it:
nnoremap <f2> :w<cr>:!start cmd /c type % && D:\Python34\python -i %<cr>
Additional tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
As your mapping only works correctly from normal mode, use :nnoremap (or extend it to support visual-mode selections, too).
Maybe Vim plugin Conque will solve your problem:
Installation instrucions are here https://code.google.com/p/conque/
To use just type :ConqueTermVSplit python -i test.py (VSplit is for vertical split - you may use horizontal)
There is no blocking of your window with python code - you may escape interactive mode and switch to your window with Ctrl+W twice
You could approach the problem from the Python angle (2.7).
Keep the file where it is (or save it with some unique name to a temporary directory) and have python load the file directly.
Go to that location in your shell and run python interactively (or have vim spin off an interpreter for you)
Import your file import demo
Experiment with what you have implemented demo.SomeModule().meth()
Make some changes in vim
Reload your python module reload(demo)
Experiment with your code again demo.SomeModule().differentMeth()
You can also have vim create a file with shortcut functions for loading/reloading the file you are working on. When vim kicks off the interpreter, you can have it set this file to the PYTHONSTARTUP environment variable, which is a file the interpreter will automatically load when it starts up. For example, you could have a function called r() to automatically reload the file you are working on.
It's also worth mentioning that reloading modules can be a little weird. If you instantiate some modules then reload the file, only new modules will use the new code; the old modules will run with the old code.

Running python scripts from DOS or python shell without path name

I was attempting to edit the registry so when I type into either the python shell or a DOS window:
python sample.py
I want it to go to the directory that I save my .py files to and run the file without me having to type:
python C:\PythonPractice\sample.py
any ideas?
For the DOS window:
set VARIABLE=yourpath
python %VARIABLE%\sample.py
So for your example you could do this:
set p=C:\PythonPractice
python %p%\sample.py
You can setup this permanently by going to "Control Panel">>"System">>"Advanced system settings">>"Environment Variables". You probably want to add a variable to your account, unless you want it to affect all of the system profiles. A restart is probably required.
#echo off
c:
CD c:\py
c:\python271\python.exe %1
Save that as py.bat in a dir on your PATH. Change c:\py to the directory of your scripts.
You can call your scripts from everywhere like this:
C:\Windows>py hallowelt.py
Hallo!
A slight improvement to Jacob's answer:
#echo off
pushd c:\py
c:\python271\python.exe %*
popd
Save this as py.cmd in one of the directories from your your PATH environment variable. Then you can call
py sample.py arg1 arg2 ...
This works with any number of arguments.
But, as wberry mentioned, you could change the working directory from inside your Python script as well, if you really need to (but I think that's a bad idea):
os.chdir(os.path.abspath(os.path.dirname(__file__))) #untested
While this isn't exactly an answer to your question, I recommend using the following pattern:
Say, I have a Python script c:\mydir\myprog.py that requires special environment variables (PATH, ORACLE_HOME, whatever) and maybe it needs a particular working directory.
Then I create a file myprog.cmd in the same directory:
#echo off
setlocal
set PATH=...
set ORACLE_HOME=...
pushd %~dp0
python %~dpn0.py %*
popd
endlocal
The pushd/popd part is for changing and restoring the working directory.
For an explanation of the %~... Syntax, type
help call
at the command prompt.
This approach gives you full control about the environment of your Python program.
Note that the python call is generic: If you need a second Python script otherprog.py, then just save a copy of myprog.cmd as otherprog.cmd.

Drag and drop onto Python script in Windows Explorer

I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target.
Is there some kind of configuration that needs to be done somewhere for this work?
Sure. From a mindless technology article called "Make Python Scripts Droppable in Windows", you can add a drop handler by adding a registry key:
Here’s a registry import file that you can use to do this. Copy the
following into a .reg file and run it
(Make sure that your .py extensions
are mapped to Python.File).
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
#="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
This makes Python scripts use the WSH drop handler, which is compatible with long filenames. To use the short filename handler, replace the GUID with 86C86720-42A0-1069-A2E8-08002B30309D.
A comment in that post indicates that one can enable dropping on "no console Python files (.pyw)" or "compiled Python files (.pyc)" by using the Python.NoConFile and Python.CompiledFile classes.
write a simple shell script (file.bat)
"C:\python27\python.exe" yourprogram.py %*
where %* stands for the all arguments you pass to the script.
Now drag & drop your target files on the file.bat icon.
With an installed python - at least 2.6.1 - you can just drag and drop any file on a python script.
import sys
droppedFile = sys.argv[1]
print droppedFile
sys.argv[0] is the script itself. sys.argv[n+1] are the files you have dropped.
Try using py2exe. Use py2exe to convert your python script into a windows executable. You should then be able to drag and drop input files to your script in Windows Explorer. You should also be able to create a shortcut on your desktop and drop input files onto it. And if your python script can take a file list you should be able to drag and drop multiple files on your script (or shortcut).
Create a shortcut of the file. In case you don't have python open .py files by default, go into the properties of the shortcut and edit the target of the shortcut to include the python version you're using. For example:
Target: C:\Python26\python.exe < shortcut target path>
I'm posting this because I didn't want to edit the Registry and the .bat workaround didn't work for me.
1). create shortcut of .py
2). right click -> properties
3). prefix "Target:" with "python" so it runs the .py as an argument into the python command
or
1). create a .bat
2). python some.py %*
these shortcut versions are simplest for me to do what i'm doing
otherwise i'd convert it to a .exe, but would rather just use java or c/c++
Late answer but none of the answers on this page worked for me.
I managed to enable/fix the drag and drop onto .py scripts using:
HKEY_CLASSES_ROOT\.py -> Set default value to Python.File
HKEY_CLASSES_ROOT\Python.File\Shell\Open -> Create a key called Command with default value "C:\Windows\py.exe" "%1" %*
CLASSES_ROOT\Applications\py.exe\open\command -> Create keys if the don't exist and set the default value to "C:\Windows\py.exe" "%1" %*
CLASSES_ROOT\Python.File\ShellEx -> create key DropHandler with default value {86C86720-42A0-1069-A2E8-08002B30309D}
That's it. Test it by dragging a file onto the python script:
import sys
args = sys.argv
print(args)
For those who use argv in .py script but still can't drag files to execute,
this could be solved by simply using Python Launcher (with rocket icon)
the script property "Open File" was set as python.exe,
which has no knowledge that the script needs command-line arguments "%*"
Refer to: https://bugs.python.org/issue40253

Categories

Resources