Accessing files in directory outside of Anaconda - python-3.x

Newb here to python/anaconda.
I'm trying to run a python script (and it reads other files local to its directory) which doesn't belong to the same directory as the Anaconda directory where I'm executing the python command there.
So I am trying to run a python script (lets say it resides in path B directory) from the Anaconda python (say path A directory). Within this script, there is a call to reading a local file so we have only the fileName.py and then the command prompt spits out this error:
C:\Users\usernameABC\Anaconda3>python C:\Users\usernameABC\Documents\GitHub\python\ThinkStats2\code\nsfg.py
Traceback (most recent call last):
File "C:\Users\usernameABC\Documents\GitHub\python\ThinkStats2\code\nsfg.py", line 165, in <module>
main()
File "C:\Users\usernameABC\Documents\GitHub\python\ThinkStats2\code\nsfg.py", line 130, in main
resp = ReadFemResp()
File "C:\Users\usernameABC\Documents\GitHub\python\ThinkStats2\code\nsfg.py", line 27, in ReadFemResp
dct = thinkstats2.ReadStataDct(dct_file)
File "C:\Users\usernameABC\Documents\GitHub\python\ThinkStats2\code\thinkstats2.py", line 2646, in ReadStataDct
for line in open(dct_file, **options):
FileNotFoundError: [Errno 2] No such file or directory: '2002FemResp.dct'
I basically just want to run the script, whether its in Anaconda or wherever is the easiest or most natural that most experienced coders would. Please help! Thanks in advance for feedback!

You can change your current working directory using the
cd (change directory) command
You can also modify the script so that you can specify where the files are that you want to access. If you show us the script that you are running we may be able to help more.

Related

Python FileNotFoundError even if the File is there

My project directory tree is something like this,
build
----fonts
--------Roboto Bold.ttf
----main.py
The following code errors out,
from pyglet import font
font.add_file("fonts\\Roboto Bold.ttf")
Also tried this code,
import os
from pyglet import font
font.add_file(str(os.getcwd())+"\\fonts\\Roboto Bold.ttf")
the error is,
Traceback (most recent call last):
File "{full_dir_name}\build\main.py", line 25, in <module>
font.add_file(str(os.getcwd())+'\\fonts\\Roboto Bold.ttf')
File "C:\python-3.8.5-amd64\lib\site-packages\pyglet\font\__init__.py", line 183, in add_file
font = open(font, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '{full_dir_name}\\fonts\\Roboto Bold.ttf'
I have tried about a day now and also seen other posts here but could not resolve...
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd(). If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:
font.add_file(str(os.getcwd())+"\\fonts\\Roboto Bold.ttf")
font.add_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "fonts/Roboto Bold.ttf"))

Reading .doc files in python on windows 10

Note: This was flagged as a potential duplicate of this, but the point of my question is that using textract doesn't work. I am looking either for (a) a way to get textract to work on windows 10 or (b) an alternate solution.
I am building a system that needs to read various types of files. I have set up pdfminer to read the .pdfs, and based on the process outlined here I installed textract, and I can now also read .docx files. However textract relies on antiword for reading .doc files and I cannot get this to work, even after following the directions here I could not find and install a working version of antiword. I do not have microsoft word installed on my machine, and I am running windows 10 with python 3.6.5. Is there any other way to read .doc files?
Here is the bug when running textract.process('d.doc') (ignore the first error, the file is definitely there):
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 84, in run
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\__init__.py", line 77, in process
return parser.process(filename, encoding, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 46, in process
byte_string = self.extract(filename, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\doc_parser.py", line 9, in extract
stdout, stderr = self.run(['antiword', filename])
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 91, in run
' '.join(args), 127, '', '',
textract.exceptions.ShellError: The command antiword d.doc failed with exit code 127
From the 'Windows installation problem' series: https://github.com/deanmalmgren/textract/issues/194#issuecomment-507243521
After following the steps to 'install' antiword, I was facing the same problem as yours.
Restarting windows after setting up the environment path variable entirely solved this exact error message for me. (This was the last error I was facing for handling .doc files with textract)
Installation instructions extracted from https://github.com/deanmalmgren/textract/issues/194#issuecomment-506065817
"Install Antiword
(I followed Reading .doc file in Python using antiword in Windows (also .docx))
Go to https://www.softpedia.com/get/Office-tools/Other-Office-Tools/Antiword.shtml
Extract to c:\antiword (yes it's required to be in the root directory)
Add location to path like we did tesseract-ocr [basically add c:\antiword to the system path (environment variable)]"
I was able to get part of the text using olefile, but olefile ultimately only handles bytes and does not handle the encoding of Word .doc files. The solution is to use LibreOffice, see my other question here

Pyinstaller does not look for the file inside the Python Virtual Environment

I want to convert Python code into standalone executable , the code uses numpy, scipy, and Pymc3 modules. I am using Python 3.6.4. I have created the project using Virtual Environment in Pycharm IDE. The command that I used to run pyinstaller is as follows:
venv/Scripts/pyinstaller --onefile src/POD.py
venv being the folder where virtual environment is stored and src being the folder where my Python source code ie. POD.py is saved. Pyinstaller runs just fine generates some warning message, but when I try to run the executable generated by pyinstaller it thrown as error
Traceback (most recent call last):
File "POD.py", line 196, in <module>
File "POD.py", line 79, in train
File "Lib\site-packages\theano\tensor\var.py", line 155, in __mul__
File "Lib\site-packages\theano\gof\op.py", line 615, in __call__
File "Lib\site-packages\theano\tensor\elemwise.py", line 482, in make_node
File "Lib\site-packages\theano\tensor\elemwise.py", line 438, in get_output_info
File "Lib\site-packages\theano\tensor\elemwise.py", line 157, in __init__
File "Lib\site-packages\theano\gof\op.py", line 1308, in __init__
File "Lib\site-packages\theano\gof\op.py", line 1331, in load_c_code
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\User\\AppData\\Local\\Temp\\_MEI126642\\theano\\tensor\\c_code\\dimshuffle.c'
[4756] Failed to execute script POD
Seems like it is trying to find the file dimshuffle.c outside of the virtual environment. In-fact the I can see that there is folder inside virtual environment which has the dim_shuffle.c code. I can find it inside virtual environment in following folder.
venv/Lib/site-packages/theano/tensor/c_code/dimshuffle,c
My question is how do I instruct pyinstaller to look for dimshuffle inside virtual environment folder?
You can,
extend the sys.path by editing the spec file.
pyi-makespec --paths=/path/to/thisdir \
--paths=/path/to/otherdir myscript.py
List the hidden imports using the spec file.
for more info refer this

Error in using pyexiv2 metadata.read() method

I am trying to follow the tutorials in the pyexiv2 tutorials page
But due to some unknown reasons, it refuses to run, and I thought of seeking some advice from you.
`#this is my code
import pyexiv2
metadata = pyexiv2.ImageMetadata('test.png')
metadata.read()`
when I try to run this code in the terminal, with the python interpreter, in the folder which contains the image, 'test.png', following error is given.
`Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pyexiv2/metadata.py", line 107,
in read
self.__image = self._instantiate_image(self.filename)
File "/usr/lib/python2.7/dist-packages/pyexiv2/metadata.py", line 74,
in
_instantiate_image
raise IOError(ENOENT, os.strerror(ENOENT), filename)
IOError: [Errno 2] No such file or directory: 'test.png'
`
This is my setup. I am running ubuntu 16.04LTS on virtual machine on a Mac book, with macOS sierra. Python version is 2.7.12.
I used this guide to install pyexiv2. when I inspect the /usr/local/lib folder, it shows that the libexiv2 is installed there.
What am I doing wrong here? and is there a better version of pyexiv2 to use?
I am fairly new to using python.:) and thanks in advance
This can be solved in two ways
1) Copy the file test.png in the same directory you have your python file with the code.
2) Provide a complete path. If, for example, you have test.png on your desktop, replace
metadata = pyexiv2.ImageMetadata('test.png')
with
metadata = pyexiv2.ImageMetadata('C:\\Users\\MYUSERPROFILENAME\\Desktop\\test.jpg')
where MYUSERPROFILENAME is your user profile. Hope this helps

FileNotFoundError when running virtualenv

When I run the command python -m virtualenv venv
I get the following output:
Traceback (most recent call last):
File "runpy.py", line 184, in _run_module_as_main
File "runpy.py", line 85, in _run_code
File "C:\python\lib\site-packages\virtualenv.py", line 2328, in <module>main()
File "C:\python\lib\site-packages\virtualenv.py", line 713, in main symlink=option.symlink)
File "C:\python\lib\site-packages\virtualenv.py", line 925, in create_environment site_packages=site-packaged clear= clear, symlink=simlink
File "C:\python\lib\site-packages\virtualenv.py", line 1147, in install_python writefile(site_filename_dst, SITE_PY)
File "C:\python\lib\site-packages\virtualenv.py", line 362, in writefile with open(dest, 'wb') as F:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\python\\sites\\venv\\python35.zip\\site.py'
When I navigate to the 'venv' folder the file structure is completely unpopulated with only C:\Python\sites\venv\Lib\site-packages (which is completely empty)
I have tried re-installing virtualenv and googling the issue with no avail. Please advise
Have you correctly installed the python on your machine? I also run into a similar issue earlier. It was caused by my python installation.
You see if you check your python path and go to the Lib folder, I assume there was no site.py. You can:
reinstalled your python, make sure it has correctly installed (check the Lib and Script folder)
correct your python path to your user/system environment: I assume you are using Window OSX, e.g: setx PYTHONHOME "C:\<your_python_path>"; add that to your path: setx PATH "%PATH%;%PYTHONHOME%"
Don't forget to add the Script path as well, if you want to access pip without python -m
Cheers!!

Resources