I am just starting to write my first Python scripts and everything was fine, until yesterday when I tried to run a Python script in cmd. Usually, I type: cd Desktop, and than file.py. It always worked, but suddenly when I type file.py, it opens up the script in Notepad++ and it doesn't run the program in cmd. I hope that someone can help..
cd Desktop, and than file.py
Try
cd Desktop
then
python file.py
Notice the keyword 'python' before file.py
Guess in your windows env, .py file already being assign to open with notepad++
Right click any .py files, click properties, change the open with to your python executable, eg. C:\python27\python.exe
OR
Explicitly specify to run it with python in cmd
cd Desktop
python file.py
Related
I'm migrating from Spyder to VSCode and would like to use it in a similar way.
One of the major problems I'm facing is to set de cwd to the folder of the file I'm working. There is a lot of question just like mine in the Google, but none of them solved my problem.
Here are my configs:
"code-runner.cwd": "${fileDirname}",
"code-runner.fileDirectoryAsCwd": true,
"terminal.integrated.cwd": "${fileDirname}",
"python.testing.cwd": "${fileDirname}",
"python.terminal.launchArgs": [
"-m",
"IPython"
],
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"python.terminal.executeInFileDir": false
What I want to do is to execute my file in IPython with the cwd setted to my file directory, to load some CSVs that are in the file folder, also be able to run some independent lines or enter some code directly in Ipython with all the variables loaded.
I don't want to just RUN the file or DEBUG, because I want to prototype some lines of code right in the IPython console with all the variables loaded.
With my configs, what I usually do is to select all the lines of the script and CTRL + ENTER. This will open a terminal with Ipython but setted to my project folder, not my file folder.
If I open a REPL, I get the same problem: Ipython opened but not set to file folder. Worse I can't use the CTRL + Enter to send some independent lines to Ipython (in this situation CTRL + Enter will cause to open a new terminal)
If I open a terminal by the Terminal menu, the console is setted to my file folder, but without python.
The closest that I got is to open the terminal from Terminal menu, and in the terminal navigate to my python and execute IPython, but then I can't run some independent lines from my code using a shortcut (most precisely like the F9 in Spyder)
My Python is not in Windows path. I'm working with files from a disk E: and my python is on a disk C:.
No Work Reason:
"terminal.integrated.cwd": "${fileDirname}", is the configuration of the default terminal. You can find out the terminal name was powershell or something others. But when you take the command of Run Selection/Line in Python Terminal, the name of the terminal was the explicit name: Python.
Impossible Reason:
Unfortunately, the Python extension seems has not provided the configuration to modify the cwd when you take the command of Run Selection/Line in Python Terminal while you can modify the cwd when you take the command of Run Python File in Terminal. As the IPython has no configuration to modify the cwd too, so it seems has not a suitable method to achieve your aim.
Workaround:
Method1: After you into the REPL mode, modify the cwd like this:
import os
os.chdir('/tmp')
Method2:
Change the configuration: "python.terminal.executeInFileDir": true
Execute the command of Run Python File in Terminal first, in order to change the terminal path.
Execute the command of Run Selection/Line in Python Terminal.
Method3:
Execute the command of Run Selection/Line in Python Terminal.
Exit out and change the path of the terminal.
Run IPython again.
I have a python file that I want to run every time the mac comes out of its sleep.
I have turned the .py file into an dmg file and placed into login items section of the mac this is in settings -> users -> login items.
My problem is that this seems to only run the DMG file when i fully logout of the the mac. I always just click the sleep option on the mac. Is there a way to run the python script each time the computer wakes up from a sleep?
#kitchen800 you can try making a shell script that runs your python program, or like another suggestion, use pyinstaller/py2app to make it into a executable application. Putting the python program itself in the Open at login will simply open the python script, not run it.
Method 1: Make it a shell script
Make a file called runpyscript.sh (the file name doesn't matter, but keep the .sh)
Open the file with a text editor/IDE and write in it
python3 path/to/file.py
Save the file and navigate to your terminal
Run the command chmod +x path/to/runpyscript.sh
You can also get rid of the .sh at the end of the file to make it executable by double-clicking
Add this shell script to "Open at login".
Now, when you login, it should (hopefully work and) open this shell script in terminal and run your python program.
Method 2: Make it an application with pyinstaller
Navigate to terminal
Put this in: pip install pyinstaller (if you haven't installed pyinstaller already)
Now put this in: pyinstaller path/to/file --onefile <other options go here>
For more on the options, you can go to this link: https://pyinstaller.readthedocs.io/en/stable/usage.html
Now, you should have a .app file. Navigate to your finder, press CMD+SHIFT+H, and go to the folder called dist. There, you should find a file_name_here.app and you can put it in Open at login.
Method 3: Make it an application with py2app
Navigate to terminal
In the command prompt, put pip install py2app
Next, write cd path/to/file.
Now, you can generate a setup.py file for your python application. In the command prompt, write py2applet --make-setup file_name_here.py
Once you have that done, you can do python3 setup.py py2app -A to construct the app in alias mode. In the same directory, go to the folder called dist in Finder and open it. You should find a .app file in it. If anything goes wrong while opening the .app file, then configure setup.py and run rm -rf build dist and the above setup.py command again until it works.
Then, go back to terminal and write python3 setup.py py2app to build the final version of the app.
Once you're done, put it in Open at login.
OS: Mac 10.14.6
Python Version: 3.8.5
New to Python and Bash so apologies if this is a dumb question but I can't find an answer anywhere. The closest I found was this answer on this thread however, I've already executed chmod +x on that file to change the permissions to allow it to be executable and I followed the instructions again and I still couldn't get it to work.
Basically I want to run Python scripts from a specified folder on my desktop (file path ~/Desktop/Python\ Scripts) through Terminal without having to change directories (out of pure laziness).
I added the folder to PATH and can see that it is listed when I run echo $PATH in Terminal. I thought that would do the trick but when I try to run the program with the command python boxprintV2.py as I usually would when I change directories I get python: can't open file 'boxprintV2.py': [Errno 2] No such file or directory
This command works fine if I change the current directory as I have been doing and I can run my program no problem but I would like to run from a new terminal window without having to change directory every time. Permissions on the file have been changed using chmod +x
Shebang from my program is #!/usr/bin/env python3.
If you run the command python <filename>, the Python interpreter will only check the current directory. Therefore, this only works if your working directory is "~/Desktop/Python Scripts", as you have already found out.
Because your script is marked as executable and it includes a shebang at the beginning of the file, you can just execute it directly from the command line by only entering boxprintV2.py. Bash will then search all directories in $PATH for this file and execute it.
Ok, I've found a workaround by creating a shell script following this answer on a different thread.
What I did was open a blank textedit file, go to format and convert it to plain text (or ⇧ + ⌘ + T which toggles rich text/plain text).
From there I typed these commands into the document as follows:
#! /bin/bash
cd ~/Desktop/Python\ Scripts
python boxprintV2.py
When I saved I didn't specify a file extension and unticked the box that said "If no extension is provided, use .txt". I'm not sure if this was necessary but I'm just detailing my exact workflow for anyone else who may have the same (laziness) problem as I do.
I then went back into a blank terminal window and entered:
chmod +x ~/Desktop/Python\ Scripts/boxprintV2to allow the shell script to be executed by all users.
From here I can just open the Python Scripts folder on my desktop, double click on the plain text file which is now a .exe and a new terminal window is opened with my Python script running!
It's literally going to save me tens of seconds of my life. I'm sure I'll waste them anyway.
I am new to programming, only two weeks in, and I am using Python 3.8. I am working my way through "Automate the Boring Stuff." One of the projects has us opening files in the cmd line and I cannot get .py files to open using cmd. When I only type python it gives me good info:It tells me this is Python 3.8.5 yadda yadda yadda. Then, if I try to type python hello.py it says 'invalid syntax.' Wait there is more. So I close cmd and reopen, then type: python hello.py and it says 'can't open file 'hello.py' [Errno 2] no such file or directory.' This is maddening. Any help is appreciated.
So, invalid syntax means there is a problem with your code. It opened the file just fine the first time you did it. If you want help with this, you'll need to post your code.
The second problem is probably because the file is in a specific folder, and you weren't in it. For example:
- ~/ #The command line starts here
- documents/
- programming/
- hello.py >This is where your file is
If you try to run python hello.py while in the home folder, it won't work because hello.py isn't in that folder. You have to go into the folder where it is.
Once you know which folders it's in (you can see a list of folders with ls on mac and dir on windows), you can go into that folder with cd folder_name. When you can type ls or dir and it one of the items is hello.py, then run python hello.py
Please note that the folder system I showed above is a guess - I have absolutely no idea where your file is.
When you type "python" in the command line, you enter the Python Interactive Shell. This means that you are able to type python code and execute it. You know you are in the Python Interactive Shell when you see >>> at the start of the line. It is saying "invalid syntax" because you are trying to run python hello.py in the Python Interactive Shell. You can type quit() and hit enter to exit the shell.
Your second issue is happening because you don't specify the directory in which the python file is located. For example, let's say your python script, hello.py, is saved on your Desktop. You need to utilize the cd command, which is short for change directory.
cd Desktop
will change your current working directory to the Desktop. If your file is located in the Desktop, you can then type
python hello.py
and your script will run.
I wrote a python program using the spotipy library. I used pyinstaller to create executables and it works fine for me. However, when I try to email it to someone (via gmail, sent through imessage) it doesn't run correctly. We are all on Mac OS X. When they try to open it with terminal, it opens terminal but the program doesn't run. I created the executable with the following command:
pyinstall -F example.py
and I sent the executable in the dist folder. I've never really tried to distribute any code before so any help would be appreciated. Thanks.
For XOS and if you gonna use pyinstaller:
pyinstaller
Put this line at the top of your python code and it will inform your operating systems to look up the binary program which needs for the execution of the python script for example it is the python interpreter.
Therefore it depends on your operating system where it keeps the python interpreter. If you have Ubuntu as operating system it keeps the python interpreter in /usr/bin/python so you have to write this line at the starting of your python script;
#!/usr/bin/python
Write above line at the top of your python code
Saving your code
Start your command terminal
Make sure the script lies in your present working directory
Type chmod +x script_name.py
Now you can start the script by clicking the script. An alert box will appear; press "Run" or "Run in Terminal" in the alert box;
Or, at the terminal prompt, type ./script_name.py
For Windows as your OS.
Make sure the python script contains:
if __name__ == '__main__':
main()
Make sure you do not have a folder with the same name as the script you are trying to turn into an executable.
Make sure you do not have any .ipynb in the same folder where you will create your executable, it will not run properly.
You can follow these instructions:
Making Executable Python