FileNotFoundError [Errno 2] when running pyinstaller executable? - python

I am trying to make my python file as an executable using Pyinstaller. After the process of conversion has finished, in my dist folder, when I click on "myApplication.exe" I get the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\Application\\dist\\myApplication\\smart_open\\VERSION'
[17416] Failed to execute script myApplication
I've already searched for answers to this but none of them have the specific error as mine which is the folder smart_open\VERSION as I have no idea what that's supposed to be.
EDIT
The smart_open folder does not even exists in my myApplication folder

Instead of using the original version of "smart_open" use this fork of the library: https://github.com/rs-trevor/smart_open
The pull request is here: https://github.com/RaRe-Technologies/smart_open/pull/344
It essentially converts the strange (extension-less) "VERSION" file into "version.py" so pyinstaller picks it up correctly
I encountered the same issue posted it within the smart_open github: https://github.com/RaRe-Technologies/smart_open/issues/345

I think you have to add the VERSION file of smart_open in the spec file. More information in the documentation.
Edit
In this case, datas line should be:
datas=[ ('c:\\python360564\\lib\\site-packages\\smart_open\\VERSION', 'smart_open\\VERSION' )],
The first part is the initial path of the file and the second the destination path (get from the error message).

Related

open with not creating and writing file

I am following a tutorial (Beautiful Soup) and I can't seem to get open with to create a .txt file as I am getting this error:
with open(f'posts/{index}.txt', 'w+') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'posts/1.txt'. I tried looking up different solutions to my problem currently running python 3.9. Is there another step I need to correct this issue?
The interpreter isn't finding the directory "posts" in your current working directory. If it exists, you are not in its parent directory. Try using the full path, e.g. rf'C:\users\myuser\posts\{index}.txt' or rf'~/posts/{index}.txt' on linux.
Or, if it doesnt't exist, add these lines:
import os
os.mkdir('posts')
You can find your current working directory this way:
import os
os.path.abspath('.')

open with mode 'ab' got FileNotFoundError (No such file or directory)

I know there is a lot of No such file or directory error. But I think this one is different.
It's easy to understand when the file was not existed and opened with normal mode read or write, or the intermediate directory is not existed.
But I got this when using append mode and the filename is a relative filename without / in the middle (it contains unicode characters).
I got the error from log file and I cannot reproduce it with same filename.
FileNotFoundError: [Errno 2] No such file or directory: 'A01_UE需求设计_V1_1_20211013160004'
... other call stack
File "pkg/xxx.py", line 304, in download
with open(file_name, 'ab') as f:
Other info:
os: ubuntu 16.04
kernel: 4.4.0-116-generic
python: version 3.8
filesystem: ext4 on lvm
Problem resolved.
I struggled with the open(f, 'ab') but never found any problem.
I have a sudden idea this morning. Maybe someone changed cwd. And I searched in the project and found it. Someone wrote code like following
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
This code changed the working dir and did not restore it. The tmpdir got removed since it was created in with context.
And I cannot reproduce it because above code is in handler of another api request.

How to add whl file on pip requirements files?

Assuming you created a whl of a proprietary project and would like to reuse it in another python project, how to indicate it via a relative path in a pip file without exposing whl online?
Although there is no direct description for this problem, from the documentation (https://pip.readthedocs.io/en/1.1/requirements.html) I have tried the following:
Indicate using -e file:
./folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl#egg=my-project==0.1.0
got> NotADirectoryError: [Errno 20] Not a directory
Indicate using file:
./folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl#egg=my-project==0.1.0
got> FileNotFoundError: [Errno 2] No such file or directory:
'/folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl'
It seems to seek the file on the root folder instead of the relative one.
Indicate without file:
folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl got> Invalid
requirement: 'folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl'
It looks like a path. Does it exist ?
It appears that if I indicate the folder instead it will work.
But as this is not the case, does anyone know how to fix it?
numpy
pandas
file:./folder/my-project-0.1.0-cp36-cp36m-linux_x86_64.whl#egg=my-project==0.1.0
Pip install whl file using relative path with success!

Call a file in another folder in Eclipse for Python project

I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)

Full File Path vs. Short OSX

I'm running OSX Mavericks but this problem has been going on since I had Snow Leopard.
When writing a script in any language, eg: Python. When I try to open a file the short
form doesn't work.
file = open('donkey.jpg')
And I get this error:
IOError: [Errno 2] No such file or directory: 'donkey.jpg'
Instead, I always have to specify the full path.
file = open('/Users/myName/Desktop/donkey.jpg')
Any ideas on why this could be happening and how to correct it?
If you specify donkey.png, it means donkey.jpg file in the current working directory. (relative path)
Make sure you're running the script in the same directory where donkey.jpg exists.
If you want specify the image file path relative to the script file instead of current working directory use following:
import os
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'donkey.jpg')
NOTE You can use __file__ only in script file. (not in interactive mode)
Your open call does not have a mode parameter. In which case, it defaults to opening the file in read mode.
Unless the file you are opening (to read) resides in the current working directory, it is completely expected that the python script throws a IOError.

Categories