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}"
Related
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,
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 want to run a Shell script using Automator in OSX. The shell script calls a Python file which is located in the folder "/Users/Tex/Python". My code is:
on run {input, parameters}
tell application "Terminal"
do shell script "/Users/Tex/anaconda3/bin/python /users/Tex/Python/MoveFiles.py $#"
end tell
return input
When I execute the automator script I get an error as follows:
Terminal got an error: /users/Tex/anaconda3/bin/python:
can't open file '/users/Tex//Python/Move_Files.py': [Errno 2] No such file or directory
I suspect that the problem is that the "/Users/Tex" folder is located on a different drive to the OSX operating system but that the Shell interpreter is not aware of this. I have tried using the full path to the python file, i.e. including the volume name (iMac Disk A) however the fact that this has spaces in it seems to create yet another issue for the Shell.
Appreciate feedback on how to make thsi work!
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave
I'm just getting started with Python and am trying to run a program from the command line, as it is done on this website under the heading "Python Program". So I made script hello.py, it's located in my computer at C:\Python27.
In the example, they run the script by typing python hello.py Guido. When I try to do this, it doesn't work. Firstly, I'm not entirely sure what is meant by the 'command line', but I'm using cmd.exe in Windows XP. I get this:
python: can't open file 'hello.py': [Errno 2] No such file or directory.
I have already specified PATH as C:\Python27.
Also, when I try to run the program from the Python shell by typing hello.py Guido I get
SyntaxError: invalid syntax.
When you start cmd.exe, the default directory is your Documents and Settings: since your file hello.py is not there, the python interpreter can't find it, thus giving you the [Errno 2] No such file or directory error. To solve that, just change your current working directory:
C:\Documents...>cd C:\Python27
C:\Python27> python hello.py Guido
Anyway, it is a good approach not to having your files inside the python directory (create a directory in your documents for python sources and use the same approach).
When you are running the python shell, you cannot explicitly call python files, so in your case it tries to run hello.py as a command (which doesn't exists) and it gives you a syntax error.
You need to locate your cmd current directory at C:\Python27:
cd C:\Python27
because the path python loads is relative. You can also use a full path:
python C:\Python2.7\hello.py
Try without "python", when you put python directory in path, it automatically connects ".py" extension with python, so there is no need in writing "python hello.py Guido"
Just go to directory where .py is located, and call "hello.py"
What's your current working directory and where is hello.py located? To execute that command, hello.py should be in the same directory from where you started the commend line (cmd.exe). Otherwise you need to the write the absolute path of hello.py (like python C:.....\hello.py Guido) instead of just the filename 'hello.py'.
I had also this problem but because of ether reason: I accidently added spaces to the names of some of the file's names, so the CMD didn't recognized the names.
for example: 'run .bat'