I had a working python file with code
form_class = uic.loadUiType("PGUI.ui")[0]
but I moved both .py file and .ui file to another directory and it doesn't work in VSCode with Error:
FileNotFoundError: [Errno 2] No such file or directory: 'PGUI.ui'
but it work when I make it .exe file with pyinstaller
How can I fix the problem?
It was my mistake about VSCode usage.
When I open folder A which has A\B\abc.py and A\B\PGUI.ui,
working directory is A, so abc.py cannot refer PGUI.ui
Related
My exe file unable to find the folder, though i have added in datas.
First i create .spec file
pyinstaller --onefile app.py --name myapp
Then i edit .spec file as follow
datas=[('models','models'),('nltkdata','nltkdata')],
Then I do
python -m PyInstaller myapp.spec
Now when i run exe, it throw this error
FileNotFoundError: [Errno 2] No such file or directory: 'models/logisticregression.pkl'
Here is directory structure
EDIT:
Its working if i call my app this way
dist/myapp
But its not working if i cd into dist
and then do
./myapp
EDIT2:
If I removed --onefile. its working. But i want one file
On windows I have to provide my data to the .spec file like this:
datas=[('./models','models'), ('./nltkdata','nltkdata')]
I found the solution. Here it is
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
os.path.abspath(os.path.join(bundle_dir, "models/logisticregression.pkl"))
source: github
I use subprocess.popen in python to put files in hdfs. it runs accurately using python on the Windows cmd. but as I use vscode to run the code, I get "FileNotFoundError: [Errno 2] No such file or directory: Error.
hdfs_path = os.path.join(os.sep,'mongo_import_export')
#put csv into hdfs
put = Popen(['hadoop','dfs','-put','mongo-hadoop/import_export.csv','/mongo_import_export'], stdin=PIPE,bufsize=-1)
put.communicate()
Knowing that my file import_export.csv is in the file in witch the code is located and mong-hadoop folder is in my local files
VSCode is running the code in a different working directory than your local CMD. Use the absolute path to the files you want to put rather than relative paths.
I am trying to open a .dat file by Pickle. Although the file is at the same folder as my .py file, when I run I get a FileNotFoundError.
(FYI: I am using VScode to run the file, however, it runs perfectly fine when I use Terminal)
here is my code
import pickle
websites_list = pickle.load(open("websites.dat","rb"))
print(websites_list)
This is the .py path:
/Users/lequangdang/Documents/WSC/WSC_2.0/changewebsiteGUI.py
here is the .dat path: /Users/lequangdang/Documents/WSC/WSC_2.0/websites.dat
Here is the error:
FileNotFoundError: [Errno 2] No such file or directory: 'websites.dat'
The relative path is base on work directory when using vscode, but is base on py file itself when using terminal.
For example the directory structure seems like:
WSC---WSC2.0---changewebsiteGUI.py
when you open WSC in vscode, pickle.load(open("websites.dat","rb")) will call the path WSC/websites.dat and that's why you have FileNotFoundError
one way to solve this is to set the vscode launch.json, add this line:
"cwd": "${workspaceRoot}/WSC2.0"
the other way is to use some function to get the abs path of pyfile itself:
os.path.dirname(os.path.abspath(__file__))
I write in CMD:
C:\ProjectStray\Model>python smth.py
But get:
python: can't open file 'smth.py': [Errno 2] No such file or directory
python C:\ProjectStray\Model\smth.py also not works
My files:
On another windows environments that works.
How can I fix that?
Both files, the .py file and a .txt file it calls with
champions_list = open('champions.txt','rU').read().split('\n')
are in the file folder C:\Users\[My Name]\Programming\[file name].
I'm calling the .py file through Command prompt and it returns the error
IOError: [Errno 2] No such file or directory: champions.txt
Has this happened to anyone else before?
When you open a file with open('champions.txt'), then the OS expects to find the champions.txt file in the current directory. The current directory is the directory of the command prompt window where you started the program. This is not (necessarily) the same as the directory where the Python script is stored.
You may be able to fix this by doing:
import os
import sys
open(os.path.join(os.path.dirname(sys.argv[0]), 'champions.txt')
This takes the full name of the script in sys.argv[0], takes the directory part, and then joins that to the file name you want. This will open the file in the script directory, not the current directory.
(Note that using sys.argv[0] in this way is OS-dependent, and works on Windows but may not work the same way on other systems.)
Just because the file is in the same folder as the script does not mean the python interpreter knows that the file is there. It is looking for a file in the cwd. You can:
Try using the full absolute path of the file; or
Add the directory containing the file using os.path.append