I tried to convert the langdetect package into an .egg file. However, when importing and using the .egg file within python code I get the following error message:
IOError: [Errno 20] Not a directory: '/workspace/langdetect-1.0.1/dist/langdetect-1.0.1-py2.7.egg/langdetect/utils/messages.properties'
After digging into the library code I found that it tries to load the messages.properties file as:
MESSAGES_FILENAME = path.join(path.dirname(__file__), 'messages.properties')
with open(self.MESSAGES_FILENAME, 'r') as f:
Which obviously does not work when the library is zipped as .egg file since the new path of the messages.properties is:
/workspace/langdetect-1.0.1/dist/langdetect-1.0.1-py2.7.egg/langdetect/utils/messages.properties
How can I change the above code so that the messages.properties is loaded from within the .egg file?
Related
first, i'd like to mention that im using python via visual studio. not sure if this information will
be relevant but this is my first time using file input so i'm not sure
basically, i have a .txt file located in the same location as my .py file. however, when i go to access it, i get an error 'FileNotFoundError: [Errno 2] No such file or directory'
is there a way to make it work or a different IDE i should use?
I tried reading a .txt file from the same location as my .py file. however, i keep getting the error 'FileNotFoundError: [Errno 2] No such file or directory'
Take into account that the script might not be running from the same path where your python script is and most probably your are not specifying the exact path of the file.
If your file is located in the same directory where your python sript is you can use the pathlib library this way to get your script to work:
import pathlib
# Some custom code
# find your current script path
current_script_path = pathlib.Path(__file__).parent
my_file_name = "my_file_name.txt"
# Specify the file path relative to your script's path
my_file_path = current_script_path / my_file_name
with open(my_file_path, "r+") as f:
print(f.read())
I'm currently doing an assignment that requires me to use CSV files in Python. I have the .csv file in the same directory as the program itself, yet it gives me the same "No such file or directory" error.
I am sure the name is not a problem. This problem has happened to me before with another file and I wasn't able to fix that either.
Code:
with open('file.csv') as file:
content = csv.reader(file, delimiter=',')
FileNotFoundError: [Errno 2] No such file or directory: 'file.csv'
I fixed this by using the OS library for python.
Change the current working directory with:
os.chdir(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
This changes Python's working directory to the one that your file is contained in.
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).
I have uploaded a simple python package in https://test.pypi.org. When I download this with pip and try yo run I get FileNotFoundError: [Errno 2] File b'data/spam_collection.csv' does not exist: b'data/spam_collection.csv'. Earlier I had issues with uploading the csv file when packaging. See my question in Could not upload csv file to test.pypi.org. Now after installing the package with pip I run pip show -f bigramspamclassifier. I get the csv file listed. Therefore, I believe the file has been uploaded. I think the issue is with reading the file in my python file in the package. What should be the path to the csv file in SpamClassifier.py?
pip show -f bigramspamclassifier
Version: 0.0.3
Summary: A bigram approach for classifying Spam and Ham messages
Home-page: ######
Author: #####
Author-email: #######
Location: /home/kabilesh/PycharmProjects/TestPypl3/venv/lib/python3.6/site-packages
Requires: nltk, pandas
Required-by:
Files:
bigramspamclassifier-0.0.3.dist-info/INSTALLER
bigramspamclassifier-0.0.3.dist-info/LICENSE
bigramspamclassifier-0.0.3.dist-info/METADATA
bigramspamclassifier-0.0.3.dist-info/RECORD
bigramspamclassifier-0.0.3.dist-info/WHEEL
bigramspamclassifier-0.0.3.dist-info/top_level.txt
bigramspamclassifier/SpamClassifier.py
bigramspamclassifier/__init__.py
bigramspamclassifier/__pycache__/SpamClassifier.cpython-36.pyc
bigramspamclassifier/__pycache__/__init__.cpython-36.pyc
bigramspamclassifier/data/spam_collection.csv
My project file structure
Path to csv in SpamClassifier.py file #This what I want to know
def classify(self):
fullCorpus = pd.read_csv("data/spam_collection.csv", sep="\t", header=None)
fullCorpus.columns = ["lable", "body_text"]
Your script is attempting to load the spam_collection.csv file from a relative path. Relative paths are loaded relative to where python is being invoked, not where the source file is.
This means that when you're running your module from the bigramspamclassifier directory, this will work. However, once your module is pip-installed, file will no longer be relative to where you're running your code from (it will be buried somewhere in your installed libraries).
You can instead load relative to the source file by doing something like:
import os
this_dir, this_filename = os.path.split(__file__)
DATA_PATH = os.path.join(this_dir, "data", "spam_collection.csv")
fullCorpus = pd.read_csv(DATA_PATH, sep="\t", header=None)
I have uploaded a package to pypi and github. I have then installed the package and tried to use it. It contains a python script which need to read from a file. I have placed both in the same directory.
pip install pycricket
from pycricket import cricket
c = cricket.Cricket()
c.query()
Query() function involves reading from a file. When I see the 'pycricket' package in library, both script as well as file are in same folder.
query():
with open('matches.csv', 'r') as f:
#code
I don't know why I get the error.
You can inspect the current working directory with:
>>> import os
>>> os.getcwd()
If your data is in a different directory (unclear from the question, but likely given the error message), then change to the directory where the data is stores:
>>> os.chdir(path_to_data_directory)