Why doesn't python find the correct root path? - python

I git cloned a project from remote git repository into a temporary directory:
/data3/min/temp/kbmb/
I created a virtual environment 'my-env' and installed it in this environment. There is a test file in this project so I ran it under kbmb/:
python nlu/tests/random_test.py
It reported an error:
Traceback (most recent call last):
File "nlu/tests/random_test.py", line 1, in <module>
import nlu
File "/data3/min/kbmb/nlu/__init__.py", line 1, in <module>
...
This part of the error is that it didn't find the correct root:
/data3/min/kbmb/
As you can see, the 'temp' directory is missing from the path in the error message. I cloned the project into the temp directory and everything should be in the temp/ directory.
What might cause this?

It seems that in your script "nlu/tests/random_test.py" the first line is trying to import nlu
If you include this before the import nlu:
import sys
sys.paht.append('/data3/min/temp/kbmb/')
then the import nlu line should work.

Related

Python linking to wrong library folder - sndfile library not found

I get the following error when trying to import the librosa library into my python project and running it in the global python environment:
Traceback (most recent call last): File
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/soundfile.py",
line 142, in
raise OSError('sndfile library not found') OSError: sndfile library not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"Bloompipe/Synthesis_Module/bloompipe_synthesis/testSynthesis.py",
line 6, in
from LSD.lucidsonicdreams import LucidSonicDream File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/init.py",
line 1, in
from .main import * File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/main.py",
line 15, in
from .AudioAnalyse import * File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/AudioAnalyse.py",
line 3, in
import librosa.display File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/librosa/init.py",
line 209, in
from . import core File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/librosa/core/init.py",
line 6, in
from .audio import * # pylint: disable=wildcard-import File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/librosa/core/audio.py",
line 8, in
import soundfile as sf File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/soundfile.py",
line 162, in
_snd = _ffi.dlopen(_os.path.join( OSError: cannot load library '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib':
dlopen(/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib,
0x0002): tried:
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib'
(no such file)
Process finished with exit code 1
I installed the libsndfile library with homebrew and also for a virtual conda environment. When trying to run the program in the conda environment it produces the following error:
Traceback (most recent call last): File
".conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/soundfile.py",
line 143, in
_snd = _ffi.dlopen(_libname) OSError: cannot load library '.conda/envs/bloompipe_synthesis/bin/../lib/libsndfile.dylib':
dlopen(.conda/envs/bloompipe_synthesis/bin/../lib/libsndfile.dylib,
0x0002): Library not loaded: #rpath/libvorbis.0.4.9.dylib Referenced
from:
.conda/envs/bloompipe_synthesis/lib/libsndfile.1.0.31.dylib
Reason: tried:
'.conda/envs/bloompipe_synthesis/lib/libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/lib/libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/lib/libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/lib/libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/../../libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/lib/libvorbis.0.4.9.dylib'
(no such file),
'.conda/envs/bloompipe_synthesis/bin/../lib/libvorbis.0.4.9.dylib'
(no such file), '/usr/local/lib/libvorbis.0.4.9.dylib' (no such file),
'/usr/lib/libvorbis.0.4.9.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"Bloompipe/Synthesis_Module/bloompipe_synthesis/testSynthesis.py",
line 6, in
from LSD.lucidsonicdreams import LucidSonicDream File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/init.py",
line 1, in
from .main import * File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/main.py",
line 15, in
from .AudioAnalyse import * File "Bloompipe/Synthesis_Module/bloompipe_synthesis/LSD/lucidsonicdreams/AudioAnalyse.py",
line 3, in
import librosa.display File ".conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/librosa/init.py",
line 209, in
from . import core File ".conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/librosa/core/init.py",
line 6, in
from .audio import * # pylint: disable=wildcard-import File ".conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/librosa/core/audio.py",
line 8, in
import soundfile as sf File ".conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/soundfile.py",
line 162, in
_snd = _ffi.dlopen(_os.path.join( OSError: cannot load library '.conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib':
dlopen(.conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib,
0x0002): tried:
'.conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/_soundfile_data/libsndfile.dylib'
(no such file)
Process finished with exit code 1
The thing is that in both cases it is looking for the .dylib files in the wrong directories. My homebrew installation is in /opt/homebrew/lib and has the files libsndfile.dylib and libsndfile.1.dylib in it and also the libvorbis.dylib file. When trying to run on the global python environment it is looking for those files in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_soundfile_data/ though.
My conda installation is in /opt/anaconda3/lib and has the files libsndfile.dylib, libsndfile.1.0.31.dylib and libsndfile.1.dylib in it and also the libvorbis.dylib and libvorbis.0.4.9.dylib file. When trying to run on the conda python environment it is looking for those files in .conda/envs/bloompipe_synthesis/lib/python3.9/site-packages/_soundfile_data/.
In both cases when looking in those site-packages directories, the _soundfile_data folder doesn't exist even when activating the hidden files. I don't know why that doesn't exist.
I tried executing:
export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib
export PYTHONPATH=/opt/homebrew/lib
To include the paths into the python path when running
Then I printed the path variables with import sys and print(sys.path), this was the output for my global python:
['Bloompipe/Synthesis_Module/bloompipe_synthesis',
'Bloompipe/Synthesis_Module/bloompipe_synthesis',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages',
'opt/homebrew/lib']
And for the conda environment I tried:
conda develop .conda/envs/bloompipe_synthesis/lib
conda develop /opt/homebrew/lib
conda develop /opt/anaconda3/lib
Then the sys.path output is:
['Bloompipe/Synthesis_Module/bloompipe_synthesis',
'.conda/envs/bloompipe_synthesis/lib/python39.zip',
'.conda/envs/bloompipe_synthesis/lib/python3.9',
'.conda/envs/bloompipe_synthesis/lib/python3.9/lib-dynload',
'.conda/envs/bloompipe_synthesis/lib/python3.9/site-packages',
'.conda/envs/bloompipe_synthesis/lib',
'/opt/homebrew/lib',
'/opt/anaconda3/lib']
Weirdly, python is still not looking in those directories when executing the librosa import.
Finally, I tried adding the path to the homebrew installation manually by putting sys.path.append("/opt/homebrew/lib") in the beginning of the python file. It still produces the exact same errors.
So my question is, why does the _soundfile_data directory not exist in my site-packages folders for the global python and the conda environment and why doesn't it include the .dylib files for libsndfile?
Secondly, why does:
export LIBRARY_PATH=/opt/homebrew/lib
export PYTHONPATH=/opt/homebrew/lib
not do that those paths appear when printing the sys.path content?
Thirdly, why does python not find the libsndfile.dylib files with the conda environment, even though I added the homebrew and the conda installation of libsndfile to the sys path with the conda develop command?
My python3.9 is installed in /usr/local/bin/python3.9 and my conda python3.9 environment is installed in /.conda/envs/bloompipe_synthesis/bin/python
I'm on a new mac with Mac OS Monterey.
Any help is greatly appreciated!
As far as I know it only works with python 3.6 and 3.7 (lucidsonicdreams), although I didn't have success on 3.6.
I had to create a virtual environment through conda and run code through Jupyter notebook. conda install tensorflow==1.15 (will not work with higher versions), python==3.7, pip install lucidsonicdreams in your new python 3.7 environment.
Make sure module versions line up with your Nvidia CUDA drivers or lucidsonicdreams won't work.

why do I get an error message 'No module named...'

Starting from a new directory, I clone the following repository:
https://github.com/deribit/deribit-api-python
I rename the cloned directory to deribit_api_python
In the base directory, I then have a one line Python file:
from deribit_api_python import RestClient
This results in:
Traceback (most recent call last):
File "my_code.py", line 1, in <module>
from deribit_api_python import RestClient
ImportError: cannot import name 'RestClient'
Why does my code generate an error?
in __init__.py file write
from .deribit_api import RestClient
or you need to change intial calling by
from deribit_api_python.derbit_api import RestClient
The best way to implement the package would be to install via pip as mentioned in the repository. pip install deribit-api and then import it via from deribit_api import RestClient

regarding using sys.path.insert and import multiple path information contained in a folder

In demo.py of this project, there is one line of code sys.path.insert(1, 'incl'). After running this line of code, we can see that sys.path has added one more extra item 'incl'. As shown from the github project, we can see there is a subfolder named as 'incl' containing some path information. I can guess that with the help of sys.path.insert, we should be able to include these path information, but running from seg_utils import seg_utils as seg fails by giving the error message of. My estimation is that 'incl' was not correctly imported into sys.path.
Traceback (most recent call last):
File "demo.py", line 52, in <module>
from seg_utils import seg_utils as seg
ImportError: No module named seg_utils

sompy Module Import Error

I can't import the sompy module even though I installed it successfully, and it appears in the modules list of my python environment:
When I try to import the sompy module using the following statement:
import sompy
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sompy'
What's wrong?
Which interpreter/IDE are you using? If using PyCharm, the problem could be that PyCharm hasn't identified a root folder for your .py project.
I would try and recreate the root folder as a subfolder and right click > Make source directory.

Why does python say it does not have the file in the directory when there is the file in the directory?

I am using python, and I am running the script that use the file in the same directory, but it keeps giving me the error saying that there is no such file.
I am using that script file as a module to enable my other script file, by the way. The file I am using as a module is from the github project : https://github.com/nik0spapp/unsupervised_sentiment (unsupervised sentiment analysis)
Traceback (most recent call last):
File "sentiment_analysis.py", line 21, in <module>
import sentiment as unsupervised_sentiment
File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/sentiment.py", line 20, in <module>
from hp_classifiers import HpObj, HpSubj
File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/hp_classifiers.py", line 16, in <module>
from lexicon import Lexicon
File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/lexicon.py", line 17, in <module>
from datasets.emoticons_patch import patch_emoticons
File "/Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/datasets/emoticons_patch.py", line 23, in <module>
emoticons_file = open("emoticons.data","r")
IOError: [Errno 2] No such file or directory: 'emoticons.data'
Any help would be the best to me as of now. Thanks!
It's possible when using relative paths instead of absolute paths in your programs that your code is looking for a file (like "emoticons.data") in the directory of the executing program (in this case, the script, NOT the module.) I would first try changing all file-paths to absolute paths in the program, or put the "emoticons.data" in your executing directory.
There is a discrepancy between your error (emoticons_file = open("emoticons.data","r")) and the source code you reference (unsupervised_sentiment/datasets/emoticons_patch.py:23 is emoticons_file = open("datasets/emoticons.data","r")). but the basic problem is the same. The module attempts to open the file using a relative path name, which only works if you are executing from the project's directory.
If you update to the code I found in the repo, you must cd to /Users/chlee021690/Desktop/Programming/Python/Recommender System/unsupervised_sentiment/ to run it.
To me, this is broken and the project maintainers should fix it.

Categories

Resources