I run Python scripts in Notepad++ using this command
cmd.exe /K "C:\InstallPython\python.exe" "$(FULL_CURRENT_PATH)"
it works, but it works not great.
When I run
exec(open("raw_ticker_list.lua").read())
it doesn't see the file, but it is in the same folder where the script lies.
When I run
import os
print(os.getcwd())
it prints
How can I make python see files in current folder?
Use this command instead:
cmd.exe /K "cd /D "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""
To recap, open the "Run" menu, select the "Run" entry, and enter the above command as "The Program to Run". Possibly "Saveā¦" it and assign a name (and keyboard shortcut) so that it permanently appears in the "Run" menu going forward.
What it does is, it opens a command window, changes the working directory to that of the script currently active in the editor (across hard drives, hence the /D parameter), then runs the Python interpreter on the script, but keeps the command window open afterwards (the /K parameter).
Use the full path to python.exe instead of just python in case it's not on the Windows search path for executables.
Related
I'm trying to make my .py script clickable!
What I Have
The CLI program I created using Python works. I open Command Prompt and enter python my_script.py and everything works as expected. I want to make it clickable so a user doesn't have to run any commands!
What I've Tried
I created a .bat file in vim that runs the python my_script.py command. When I click it, the desired behavior is not achieved. Command Prompt opens for about a millisecond then closes.
My Question
How can I create a better .bat file so that my CLI program works with the click of a button?
Python, for better or worse does not allow you to specify the path to the script, and will not search the path for the script.
The official word, and I hate to say this, is that you must associate the Python.exe executable you want to run python scripts with the extension .py and then you can double click your script.py or type \path\to\script.py into the CMD prompt, and it will run the script.
I hate this from a standpoint of not wanting to accidentally execute a script by clicking it, and from the perspective of wanting to have multiple versions of python installed and choosing which one would do the execution.
There is an (IMHO) annoying (Kludgy) work-around.
You can create a File type for each instance of Python, you might have, and then associate it with the .py
so you could create the FTypes you need and associate them in a script at the time of to launch the script from the batch file:
FTYPE Py23=C:\Path\To\Pthon-23\python.exe %*
FTYPE Py38=C:\Path\To\Pthon-38\python.exe %*
Then your script could do this:
#Echo off
ASSOC .py=Py38
"C:\Path\To\Script.py"
ASSOC .py=
Python supports being called in a mode where it can ignore the first line of the script with -x. This coupled with a wrapper batch file means you can actually put the entire Python script in a batch file.
This little script shows how this can be done. It's a self-contained
#echo off
rem = """ # This is run in the batch file to launch Python
python -x "%~f0" %*
goto endofPython
rem = """
# Your python code goes here ..
def main():
print("Hello from Python")
if __name__ == "__main__":
main()
rem = """ # This section is run after Python is done, here just
# just run pause to keep the console window open
:endofPython
pause
rem = """
#echo off
start c:\Python27\python.exe C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py
PAUSE
My script in tool.py is correctly working in PyCharm IDE, this bat is not working.
Note : file path and python path is correct.
Any other option to run python script independently
Try removing the start from your batch file - this opens a separate window for the console application python.exe, and when python.exe exits, it will close the console window. By eliminating start, it runs python.exe in the same session of cmd that the batch file is running in, and allows you to read any output from python.exe before exiting (because of the pause command).
Python installations by default associate .py files with it and issues a command to run the interpreter PATH_TO_PYTHON\python.exe "%1" %*
So you should not need to call python.exe manually to call the script. You also do not need the start command at all.
Not sure why you need the batch to call a python script, but this would be perfect. always put the full path in quotes in case you hit whitespace in the paths.
#echo off
"C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py"
pause
I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you
I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.
I have some difficulties trying to run the python programs from cmd, or command-line. When i double click the python program it display the cmd for some seconds in fact less than 3 Seconds, and cmd disappeared. I can only run by right clicking on it then edit in idle and run model command. Only from the python shell. And how to give path for python shell to find applications, for instance just type the program name and it runs. Do python have GUI as exe programs?
First make sure python.exe is in your path.
Then type in the command prompt (cmd) python "path\to\program.py"
If you want to just type the progam name, you have two options.
Add the folder containing your program to your path.
Right click in the folder while holding shift, and then click "open command prompt here"