Desktop Launcher for Python Script Starts Program in Wrong Path - python

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.
The problem is that the script will be launched in the wrong path - namely the home folder instead of the directory it is placed in. Thereby it can not find other vital files accompanying it in its folder and hence does not work.
To examine that misbehaviour I created a short script to check the folder a python script is executing in:
#!/usr/bin/env python
import subprocess
import time
subprocess.call(["pwd"], shell=True)
time.sleep(7) # to get a chance to read the output
Executing it from its own folder gives the output:
/home/myusername/PythonProjects
I am setting a desktop launcher via Nemo's menu. Now executing the same script yields:
/home/myusername
I do not understand this behaviour. How could I create a working desktop launcher for my python script?

The page describes the format of .desktop files.
You may note the "Path" element, which specifies the working directory for the file to be run in. In your case you would want a desktop file that specified
Path=/home/myusername/PythonProjects
If Nemo doesn't allow you to set the Path element you may need to edit the desktop file by hand. The .desktop files are text files and you can probably find them in /home/myusername/.local/share/applications/

Related

Python file always runs at C:/Users/User and not in the working directory

When I'm launching python file in VS Code that for example located at C:\Users\andre\Desktop\NewFolder\Something.py or D:\Pythons\Something.py
I'm getting C:\Users\andre via os.getcwd() and not the directory where python script is located.
VS Code takes the open folder as the workspace. So please open the whole folder in VS Code, not just a .py file, then VS Code will not be able to find your workspace and will use the computer user directory as the workspace by default. So naturally you get the wrong script path.
Example:
Open the folder as a workspace:
Just open a .py file:
VS Code open folde:

Can't run Python script from Terminal without changing directories even though directory is included in PATH

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.

Making a Shortcut for Running Python Files in a Folder with CMD.exe

To run Python files (.py) with Windows CMD.exe, I SHIFT + Right Click on my project folder which contains all of my Python code files. Doing this shows a menu containing the option Open command window here, which I click to open CMD.exe with the prompt C:\Users\Name\FolderLocation\ProjectFolder>. I then type the python command, and the file I want to run in my project folder (python MyFile.py) which runs the file, of course.
What I would like to know is if there is a way I can setup a shortcut to open CMD.exe with my project folder opened/ being accessed so then all I have to do is type python and the file name? Thanks
Create a shortcut to cmd.exe.
Get properties on that shortcut, and there's an entry for the current working directory. (They change the name of that field every other version of Windows, but I believe most recently it's called "Start In:".) Just set that to C:\Users\Name\FolderLocation\ProjectFolder.
Now, when you double-click that shortcut, it'll open a cmd.exe window, in your project directory.

Running Python Files from Pycharm Project Folder with Windows Command Line

I have watched numerous tutorials on how to run Python files using the windows command line (CMD.exe), but in all of the tutorials the video makers were accessing PythonXX from Program Files and also their Python files from Program Files. I use Pycharm and have my single Project Folder in my Documents library. If possible I would like to run those files from the project folder. If not, though, is there any way to relocate a Pycharm Project Folder and Pycharm will still recognize it as where I store my files?
Thanks!
EDIT:
YES, I am aware that I can run my Python Files in Pycharm, but I would like to use windows CMD for full screen and so I can watch over it while I code.
To get to a directory with cmd simply hold shift while right-clicking the Documents folder. You should get an option
Open command window here
To run python files, enter into cmd:
python filename.py
Modify this command to include arguments if necessary.

Using getcwd when running in Eclipse

When running a project in eclipse the eclipse saves a copy of the .py file inside the workspace defined in project creation. But the file that imported to project can be in other location.
If using os.getcwd() and running the script from the command line, the return value would be the real path of the script file, but when running the script from inside eclipse, the return value would be the path of the workspace, where eclipse saves a copy of the original file.
So the question is, since I run my script during debugging from eclipse, how can I verify that it works before moving to real environment? Is there other way other than getcwd()?
Thanks,
Isaac
os.getcwd() returns the current working directory.
When running a Python program from Eclipse you can specify what the working directory should be on the Arguments tab of the Run Configuration. This will let you override the value from the project folder (or whatever the default might be.)

Categories

Resources