I have notepad++ installed on win7, and have installed the notepad++ exec plugin. Whenever I execute the .py script I am running, i get the following error:
C:/Python27/python.exe ""
Process started >>>
C:\Python27\python.exe: can't find '__main__' module in ''
<<< Process finished.
================ READY ================
My index.py script is as follows:
text = "Hello World"
print text
The Notepad++ execute script (F6) is as follows:
C:/Python27/python.exe "$(C:/Python27/python.exe)"
This is the correct path to python.exe on my system. In the windows environmental variables, I have also added this to the PATH. Also, I have booted up the python.exe command prompt and have gotten the script to work by manually typing it in. I have done a directory search of C:/Python27, and the main.py file exists.
What is going on that is wrong here? Why can't python find the __ main __ module??
EDIT:
Nevermind this, I'm an idiot.
So I had misinterpreted the setup for the notepad++ exec (F6) script:
C:/Python27/python.exe "$(C:/Python27/python.exe)"
It should have been this instead:
C:/Python27/python.exe "$(FULL_CURRENT_PATH)"
Of course it couldn't find the main.py file because it wasn't going to FULL_CURRENT_PATH.
Carry on.
Basically, it's because the 1st argument in your script has been missinterpreted.
That is to say, with the code
C:/python27/python.exe "$(C:/python27/python.exe)"
You're doing something roughly equivalent
C:/python27/python.exe
What you need to use is:
C:/Python27/python.exe "$(FULL_CURRENT_PATH)"
Note: Don't substitute "$(FULL_CURRENT_PATH)" for something else - put that in verbatim - it's a predefined variable of the NppExec plugin. It may be considered shorthand for the full-path of the file in the currently open tab.
When I execute the script above (on a file called tmp.py, in my c:\xampp\htdocs\enhzflep folder), I get this displayed in the console:
C:/Python27/python.exe "C:\xampp\htdocs\enhzflep\tmp.py"
CreateProcess() failed with error code 2: The system cannot find the
file specified.
Which is just fine, since I don't have python installed on this machine. :)
EDIT: Just saw your edit. It seems I'm the greater fool!
Related
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.
Question: In command line, how do I call a python script without having to type python in front of the script's name? Is this even possible?
Info:
I wrote a handy script for accessing sqlite databases from command line, but I kind of don't like having to type "python SQLsap args" and would rather just type "SQLsap args". I don't know if this is even possible, but it would be good to know if it is. For more than just this program.
You can prepend a shebang on the first line of the script:
#!/usr/bin/env python
This will tell your current shell which command to feed the script into.
You want a shebang. #!/path/to/python. Put that on the first line of your python script. The #! is actually a magic number that tells the operating system to interpret the file as a script for the program named. You can supply /usr/bin/python or, to be more portable, /usr/bin/env python which calls the /usr/bin/env program and tells it you want the system's installed Python interpreter.
You'll also have to put your script in your path, unless you're okay with typing ./SQLsap args.
Assuming this is on a unix system, you can add a "shebang" on the top of the file like this:
#!/usr/bin/env python
And then set the executable flag like this:
chmod +x SQLsap
Another way to do this would be to package your python script.
Then all you would have to do is run $ pip install package-name and simply running $ package-name from any directory would execute your python script.
I would recommend this method since it enables you to quickly share/install/use/remove your python script.
I wrote a simple script QuickPackage that can instantly create and upload python package for your awesome script by just entering the path of your python script.
Example:
$ pip install quickpackage
Usage :
Usage
Simply run quickpackage and enter the path of your python script.
Example:
❯ quickpackage
Enter name: quickpackage
Enter version: 1.1
Enter description: Instantly create and upload python package for your script
Enter github url: https://github.com/yask123/quick-package
enter author: Yask Srivastava
Enter email: yask123#gmail.com
Path of python script file: run.py
running register
....
running upload
Submitting dist/quickpackage-1.1.tar.gz to https://pypi.python.org/pypi
Server response (200): OK
Now simply execute your script by running$ quickpackage (In this case)
On Windows or DOS you can come close by putting your python code into a .BAT file like this.
rem = ''' this line interpreted by BAT and PYTHON
#REM BAT-ONLY code begins now inside PYTHON comment
#echo off
python "%~dpnx0" %*
goto :eof
#REM CMD-ONLY code ends
'''
import sys
print sys.path
Unfortunately, I can't get rid of the first line message on the output, but you could always change the text after ''' to be something like the application's name and people wouldn't notice.
Ideally, you wouldn't put lots of Python code in the .BAT file but would import a .py file and then run its .__main__ method.
In unix, you use a shebang line at the start of your script:
#!/usr/bin/env python
make the file executable:
chmod +x arbitraryname
and put it in a directory on your PATH (can be a symlink):
cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname
In windows, files like *.py has been set to be open with python.exe by default. (If not, you can set manually.) So you can run *.py files in console directly.
Note that:
In windows, text new line code is "\r\n", but in unix, it is "\n". If your python script is windows format, then executing in unix will report "No such file or directory" error.
To repair this problem, just replace all "\r\n" with "\n" in unix will be ok.
I would like to use a python script anywhere within command prompt. This is possible in unix, but I couldn't find anything for windows. Do I have to convert it to .exe?
Edit: not sure why this is being downvoted, maybe it's a silly question but I can't find any similar threads here and I can't be the first person to want to execute .py scripts from their path...
Edit 2: I think my wording was unclear. I would like to know a method to execute python scripts in Windows without needing to specify python path/to/script.py every time. Here is a solution on Linux where the shebang statement invokes the python interpreter, and the script in question can be easily placed in bin: How do I install a script to run anywhere from the command line? . Does there exist a solution like this for Windows?
Here's a solution for running myScript.py:
add to the myScript.py file a first line #!python (or #!python3 if you want to use Python 3)
For instance:
#!python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
change the "opens with" property of myScript.py to py.exe (to find where it is use where py-- I have it in C:\Windows\py.exe)
put the script myScript.py somewhere in your Windows path
and now you should be able to type myScript.py anywhere in a command prompt and run the Python script with your chosen Python version.
See: https://docs.python.org/3.6/using/windows.html#from-the-command-line
I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.
Question: In command line, how do I call a python script without having to type python in front of the script's name? Is this even possible?
Info:
I wrote a handy script for accessing sqlite databases from command line, but I kind of don't like having to type "python SQLsap args" and would rather just type "SQLsap args". I don't know if this is even possible, but it would be good to know if it is. For more than just this program.
You can prepend a shebang on the first line of the script:
#!/usr/bin/env python
This will tell your current shell which command to feed the script into.
You want a shebang. #!/path/to/python. Put that on the first line of your python script. The #! is actually a magic number that tells the operating system to interpret the file as a script for the program named. You can supply /usr/bin/python or, to be more portable, /usr/bin/env python which calls the /usr/bin/env program and tells it you want the system's installed Python interpreter.
You'll also have to put your script in your path, unless you're okay with typing ./SQLsap args.
Assuming this is on a unix system, you can add a "shebang" on the top of the file like this:
#!/usr/bin/env python
And then set the executable flag like this:
chmod +x SQLsap
Another way to do this would be to package your python script.
Then all you would have to do is run $ pip install package-name and simply running $ package-name from any directory would execute your python script.
I would recommend this method since it enables you to quickly share/install/use/remove your python script.
I wrote a simple script QuickPackage that can instantly create and upload python package for your awesome script by just entering the path of your python script.
Example:
$ pip install quickpackage
Usage :
Usage
Simply run quickpackage and enter the path of your python script.
Example:
❯ quickpackage
Enter name: quickpackage
Enter version: 1.1
Enter description: Instantly create and upload python package for your script
Enter github url: https://github.com/yask123/quick-package
enter author: Yask Srivastava
Enter email: yask123#gmail.com
Path of python script file: run.py
running register
....
running upload
Submitting dist/quickpackage-1.1.tar.gz to https://pypi.python.org/pypi
Server response (200): OK
Now simply execute your script by running$ quickpackage (In this case)
On Windows or DOS you can come close by putting your python code into a .BAT file like this.
rem = ''' this line interpreted by BAT and PYTHON
#REM BAT-ONLY code begins now inside PYTHON comment
#echo off
python "%~dpnx0" %*
goto :eof
#REM CMD-ONLY code ends
'''
import sys
print sys.path
Unfortunately, I can't get rid of the first line message on the output, but you could always change the text after ''' to be something like the application's name and people wouldn't notice.
Ideally, you wouldn't put lots of Python code in the .BAT file but would import a .py file and then run its .__main__ method.
In unix, you use a shebang line at the start of your script:
#!/usr/bin/env python
make the file executable:
chmod +x arbitraryname
and put it in a directory on your PATH (can be a symlink):
cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname
In windows, files like *.py has been set to be open with python.exe by default. (If not, you can set manually.) So you can run *.py files in console directly.
Note that:
In windows, text new line code is "\r\n", but in unix, it is "\n". If your python script is windows format, then executing in unix will report "No such file or directory" error.
To repair this problem, just replace all "\r\n" with "\n" in unix will be ok.