I just switch laptops and everything seems to be normal except this. When I open CMD and try to run a file like py filename.py
it comes up with an error
C:\Users\john\AppData\Local\Programs\Python\Python37\python.exe: can't open file 'filename.py': [Errno 2] No such file or directory
It defaults to the python.exe file. It does it for every file that I try to run as well. I am in the correct directory (the directory where the actual file is in) but it keeps defaulting to that error. I am running windows 10 with python 3.7.3.
You may have to change the default directory where your terminal looks for the python.exe file and its packages. You must change both. One variable points to the python[version] folder and the other points to the python[version]\scripts folder.
This page shows you how to redirect your terminal in windows:
https://superuser.com/questions/1399544/how-to-change-default-python-executable-on-windows-10/1399546
Another tip is that you must change these two identical directories for both the "user variables" and the "system variables" listed in the section below it. I believe the Python installation has probs with this, especially if it's a reinstall or a failed update. And on Windows in general.
Best,
Related
I seem to be having a vscode related issue. I am doing the open() function but no matter what I ask it to do it gives me a directory error. The file that I want the python script to interact with is in the same folder so it should work but when I do "import os" and "os.getcwd()" the directory it says I am in is Desktop. (the script and file are both in the "/Desktop/Python/File Handling" directory)
It seems the script is stuck at the Desktop directory when I try to run it from vscode. If I run it by doing python3 "name of script" command in the kali linux terminal it works fine and if I check my directory again with os.getcwd() it says the correct one (/Desktop/Python/File Handling).
So I believe it's something with vscode as it literally just randomly happened one day. Yesterday my scripts were working fine and now all the ones I run from vscode, that are supposed to interact with the files in their respective folders, don't work. The vscode terminal gives me this code and as you can see it does the cd command at the start, which I believe might be the issue for why it always looks at files in the Desktop directory but I do not know how to make it stop doing that.
$ cd /home/kali/Desktop ; /usr/bin/env /bin/python /home/kali/.vscode/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 41017 -- /home/kali/Desktop/Python/File\ Handling/File\ Handling\ 2.py
Traceback (most recent call last):
File "/home/kali/Desktop/Python/File Handling/File Handling 2.py", line 3, in <module>
f = open("apple.jpeg", "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'apple.jpeg'
Lastly, I know about the os.chdir(r"/home/kali/Desktop/Python/File Handling") function and it fixes the issue but I do not want to have to write this command at the top of every script that is supposed to interact with the files in the folder it is in by default and because this issue just randomly came up out of nowhere the next day I opened vscode and ran my script from yesterday (without changing any code or vscode settings.)
P.S. I am using a VM as well if that helps.
This is caused by vscode using workspace as root floder.
This will lead to a problem. When you use the os.getcwd() method in the deep directory of the workspace, you will still get the workspace directory.
You can open your settings and search Python > Terminal: Execute In File Dir then check it.
You can also use debug mode and add the following to your launch.json:
"cwd": "${fileDirname}"
The situation
I am writing a python program for other people, which installs itself on user's machine. Installation works fine. But after installation, clicking on the software icon in window manager doesn't run the software.
Upon closer investigation, I found out there is something wrong with HOME variable in DesktopEntry file.
The question
I expect clicking on icon of this software should run it, but it doesn't. What is wrong?
Found a duplicate question asked here but still no answer.
Info
This is the technical part of .desktop file you may need to know about, omitting Exec and Icon lines:
Version=1.0
Type=Application
Terminal=true
StartupNotify=true
X-SingleMainWindow=true
Trial and error of Exec and Icon:
Each block pair below shows a modified .desktop part and corresponding terminal output.
Attempt 1
Exec=python $HOME/.local/share/myProject/main.py
Icon=$HOME/.local/share/myProject/icon.svg
python: can't open file '/home/user0/$HOME/.local/share/myProject/main.py': [Errno 2] No such file or directory
Attempt 2
Exec=python ~/.local/share/myProject/main.py
Icon=~/.local/share/myProject/icon.svg
python3: can't open file '/home/user0/~/.local/share/myProject/main.py': [Errno 2] No such file or directory
Attempt 3
Exec=python $USER/.local/share/myProject/main.py
Icon=$USER/.local/share/myProject/icon.svg
python: can't open file '/home/user0/$USER/.local/share/myProject/main.py': [Errno 2] No such file or directory
If command of Exec entered in Terminal, error message shows two usernames in its path like /home/user0/user0/.local...
Attempt 4
Omitting the "extra" part...
Exec=python /.local/share/myProject/main.py
Icon=/.local/share/myProject/icon.svg
obviously doesn't and shouldn't work, as expected.
python: can't open file '/.local/share/myProject/main.py': [Errno 2] No such file or directory
Notes
When the command from Exec in first two attempts is called from terminal directly, python runs the script normally. But I don't want to make the user open terminal. Clicking on app icon should start it immediately.
In none of above attempts, was the icon shown (only Name). So I finally moved it to ~/.local/share/icons and written its name in place of path. This solved the issue for icon. Now I see both name and icon of my App but it still doesn't run.
Command desktop-file-validate in terminal returns nothing for .desktop file.
I am trying to run python on Ubuntu. ADDED: It's a double boot system with Windows.
If I type python on the shell, it opens python. But I want to run a python file.
I have my python (.py) file saved on windows Desktop.
On Windows when I run a .py file in my command prompt, I just have to change directory to Desktop then type python myfile.py and the code runs successfully.
When I try to do that same thing in Ubuntu, it does not work. I made a Desktop directory in Ubuntu using the mkdir function.
Now, when I type python myfile.py, I get the error:
python: can't open file 'myfile.py': [Errno 2] No such file or
directory
I tried typing python on the Ubuntu shell then dragging the myfile.py file (~$ python C:\Users\username\Desktop\myfile.py), it used to run and then close the answer right away, but now I would get the error
python: can't open file 'C:UsersusernameDesktopmyfile.py': [Errno 2]
No such file or directory
Can anyone tell me what are the exact steps I need to do to fix this?
EDIT:
Here is what I am writing from the answers below:
~$ python /home/username/Desktop/myfile.py
Yet, I am getting this error:
python: can't open file '/home/username/Desktop/myfile.py': [Errno 2] No such file or directory
EDIT 2**
So here is something new:
if I write
python /home/username/Desktop myfile.py
I get this error
/usr/bin/python: can't find 'main' module in '/home/username/Desktop
If I write
python /home/username/myfile.py
I do not get an error, but I do not get any output either. Ubuntu just goes to the next line $
Strange on Ubuntu your path starts with c:\...
On Ubuntu user folders are usually in /home, user folder can be referenced by ~, so IMHO python ~/Desktop/myfile.py should work in your environment.
EDIT: noted you made Desktop folder, not original Desktop, that way when you are in that folder type pwd it will show full path, then put it in python PATH/myfile.py (and just in case you may type ls to show list of files in current folder on Linux to check you are indeed in the right folder where your program is).
ADDED: after discussion it turned to be double boot system, mount showed mounted windows disk and file.py was found and run!
Backslash '\' is an escape character, in Unix it will not be used if you put it in a path. It results in your error of path not found.
Use slash '/', your code should run.
It is weird that you have a path for windows, in Unix you should not have this type of path...
Open a terminal, go to the folder with your python script. Use pwd in the terminal to know the exact location and then copy the path and use the following (I take an example here):
python PATHTOYOURPYTHONSCRIPT/mypythonscript.py
You're trying to use windows path representation in Linux. Windows and Linux has different path representations.
In windows, you can use C:\ but in Linux it's just a / which is used to denote the root directory.
In the terminal, type 'pwd' where your python file is present in Ubuntu and you'll see the output as '/home/username/Desktop' which is not like windows.
So you need to run like, 'python /home/username/Desktop/my file.py'.
If you need to access the file present in the windows partition, you need to mount the windows partition. This can be done using the Files app present in Ubuntu. After that, you can navigate to /mnt/media/ and find your file.
I am very new to this and struggling with the basics. I have a csv file /home/emily/Downloads/Roger-Federer.csv The textbook says that I need to "extract the file and download it to my current directory" on JupyterLab (I am using Python). What does this mean? How do I do this? Thank you
Every running program, including JupyterLab, has a "working directory" which is where it thinks it is on your computer's file system. What exactly this directory is usually depends on how you launched it (e.g., when you run a program from terminal, its working directory is initially the folder your terminal was in when you ran the command, but it's possible for a program to change its own working directory later).
Your file path indicates you're on Linux, so I'd suggest opening a terminal in your JupyterLab and running pwd to have it print out its current directory. (You can also run !pwd in any open notebook if that's easier.) You should then copy your CSV file to that directory.
If you do that, then from your Python code, you can just open the file locally, like open('Roger-Federer.csv') or pandas.read_csv('Roger-Federer.csv'). You don't have to move the file to open it from Python, though, you can just give it the entire file path, like open('/home/emily/Downloads/Roger-Federer.csv'), and that'll work just fine too.
I've just started a class that uses Python. At this point, I'm a complete beginner. I running Windows 10 via Parallels on an iMac. I'm have trouble organizing the directories within the "Python36-32" directory that is located on the VM (Windows). If I place a .py file in the main directory, I can run the file through the command prompt and see something like.
c:\Program Files\Python36-32>python first.py
hello
However, if I try to organized the file the file isn't read at all. For example, if I wanted the "first.py" file to run from a "web221" (the name of my class) subdirectory of "python36-32" and then try to open "first.py", I get:
C:\Program Files\Python36-32\web221>python first.py
'python' is not recognized as an internal or external command,
operable program or batch file.
I'd really like to keep all of the .py files I create for my class organized for obvious reasons. Any help would be appreciated.
When you execute from the one directory, the Python executable is present and Windows has no issue finding what to execute.
From the other directory though, Windows attempts to find the executable in your PATH and cannot see it.
Here is some help from the Python 3 docs:
https://docs.python.org/3/using/windows.html#excursus-setting-environment-variables
Specifically, try this as a test:
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt.
To temporarily set environment variables, open Command Prompt and use the set command:
C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
C:\>python
However, this will only temporarily add the executable to your path. Try it to see if it works, and if it does, then you'll have the information you need to add it to your PATH variable permanently. Note: your directory is named differently, so you'll need to replace Python 3.6 with the name of the directory where Python is installed.
For the task of editing the PATH such that the changes persist from one command prompt session to the next, there's a related SO question that might be helpful:
How to add to the pythonpath in windows 7?