Import caffe error - python

i compiled caffe successfully in my ubuntu machine but cannot import in python.
Caffe is installed /home/pbu/Desktop/caffe
i tried adding the /home/pbu/caffe/python path to sys.path.append, still not working
i am trying to import caffe
root#pbu-OptiPlex-740-Enhanced:/home/pbu/Desktop# python ./caffe/output.py
Traceback (most recent call last):
File "./caffe/output.py", line 13, in <module>
import caffe
File "/home/pbu/Desktop/caffe/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver
File "/home/pbu/Desktop/caffe/python/caffe/pycaffe.py", line 10, in <module>
from ._caffe import Net, SGDSolver
ImportError: No module named _caffe

This happens when you have not run make for the python files separately.
Run make pycaffe soon after running make in the Caffe directory.
You may have to set the path to the python library correctly in Makefile.config

Adding to the above best answer. After you run make for python files by running make pycaffe where you ran your previous makes. Then you have to export that python path by running export PYTHONPATH=<path-to-caffe>/python. You can choose to run this everytime before running a python code which utilizes caffe or add it to your ~/.bashrc.

Well, I use the cmake-gui for making Caffe. There you need to set the Python paths to the Anaconda-python:
PYTHON_EXECUTABLE <path_to_anaconda_home>/bin/python2.7
PYTHON_INCLUDE_DIRECTORY <path_to_anaconda_home>/include/PYTHON2.7
PYTHON_LIBRARY <path_to_anaconda_home>/lib/libpython2.7.so

You should build caffe and pycaffe using the command:
cd $FRCN_ROOT/caffe-fast-rcnn
make -j8 && make pycaffe
and before the compilation, you should create a Makefile.config file and set the corresponding library path, such as python.
More details are presented on the web: bgirshick/py-faster-rcnn.
What's more, when I run the "Beyond the demo" section, it seams that if I Create a symlink of the folder "VOCdevkit" as "VOCdevkit2007" which turns out to be "can't find the dataset". So, I change the folder name as "VOCdevkit2007", and it runs well.

I posted my Caffe install notes (my architecture: Arch Linux x86_64 | Intel i7 CPU ...) in an Anaconda Python 2.7 virtual environment here:
Caffe Installation Notes
https://gist.github.com/victoriastuart/fb2cb22209ccb2771963a25c06221213
I also encountered the (downstream) "Import caffe error," for which I needed to resolve my $PYTHONPATH to complete the make compilation and get Caffe finally installed, and also to be able to import it (in Python).

Related

ImportError: No module named pygments.styles when attempting to run Pythontex

When I try to run pythontex via my command line((base) Zachs-MacBook-Pro:mat_300 zachmaurus$ pythontex pythontex.tex) the following occurs:
Traceback (most recent call last):
File "/Library/TeX/texbin/pythontex", line 50, in <module>
import pythontex2 as pythontex
File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex2.py", line 61, in <module>
from pygments.styles import get_all_styles
ImportError: No module named pygments.styles
How do I go about solving this issue? I have downloaded pygments with pip install, but for whatever reason when I attempt to run the script the module pygments.style cannot be found.
Solved this issue by adding pythontex_install.py to my PATH using export PATH=$PATH:/usr/zachmaurus/Downloads/pythontex/pythontex_install.py in MacOS Terminal. To ensure that pythontex_install.py has been added to PATH environment run echo $PATH and it should appear as one of the variables. After having added pythontex_install.py to PATH, I now get the following result when I run pythontex pythontex.tex to compile my document:
This is PythonTeX 0.16
--------------------------------------------------
PythonTeX: pythontex - 0 error(s), 0 warning(s)
I solved this problem by explicitly call the interpreter I want, wenn I compile the document.
pdflatex -interaction=nonstopmode document.tex
python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
or for any python interpreter you want:
pdflatex -interaction=nonstopmode document.tex
<path_to_python>/python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
Of course Pygments module schuld be installed by this interpreter. This worked very good for me on macOS.
I got the same problem when I tried to use pythontex on my macbook (on macOS Catalina 10.15).
What I suggest below must be (if used) used with precaution :
I tried so many solutions and what actually worked for me was this :
I made sure that pygments was installed on my machine (I ran 'pip install pygments' on the terminal)
Then I changed the first line of the files pythontex, pythontex2 (because the problem seemed to come from there) and pythontex3
Theses files (if you are using macOS Catalina 10.15 like me) should be in
/usr/local/texlive/2021/texmf-dist/scripts/pythontex
(if you installed TexLive2021) -> you should be able to retrieve this path when you try to compile your tex file anyway...
The first line was replaced by #!/usr/bin/python3 (to ensure that python3 was used instead of python 2.7 which is used by default by macOS) inside the 3 files pythontex, pythontex2 and pythontex3 then :
I ALSO added this line (see the screenshot below) in pythontex3 :
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/')
to ensure that python found the module pygments
(because I installed python3.9 on my machine and the folder "pygments" and its subfolder "styles" was present in the folder "site-packages")
pythontex3 file
So now everything is working fine again and I can use pythontex =)

Import failure when calling Anaconda Python from Windows shell

I want to call my Anaconda Python from the Windows shell. The goal is to eventually call it from MATLAB using a system call, so I have to use the plain Windows shell, and not the "Anaconda prompt".
I have a simple Python script that I want to run, but it doesn't get past the import statements:
> "C:\ProgramData\Anaconda3\python.exe" lhs.py
Traceback (most recent call last):
File "lhs.py", line 8, in <module>
import numpy
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.
EDIT
I made a simplified Python script (error.py) containing only one line:
import numpy
I then call it as:
"C:\ProgramData\Anaconda3\python.exe" error.py
TL;DR
C:\ProgramData\Anaconda3\condabin\activate.bat YOURENVIRONMENTNAME && python N:\Temp\lhs.py
Long Answer
There is a similar issue reported on github. I found this by searching for the last few lines of your message. It is about this:
File "C:\Users\hanna\Anaconda3\lib\site-packages\tensorflow\python_init.py",
line 47, in
import numpy as np
File "C:\Users\hanna\Anaconda3\lib\site-packages\numpy_init_.py", line 140, in
from . import _distributor_init
File "C:\Users\hanna\Anaconda3\lib\site-packages\numpy_distributor_init.py",
line 34, in
from . import _mklinit
There, they link to a conda troubleshooting page:
Error messages like
Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll
Cause
NumPy is unable to load the correct MKL or Intel OpenMP runtime
libraries. This is almost always caused by one of two things:
The environment with NumPy has not been activated.
Another software vendor has installed MKL or Intel OpenMP
(libiomp5md.dll) files into the C:\Windows\System32 folder. These
files are being loaded before Anaconda's and they're not compatible.
If you are not activating your environments, start with doing that.
So let us try this first:
C:\\ProgramData\\Anaconda3\\condabin\\activate.bat
conda run -n yourenvironment C:\\path\\to\\your\\pythonfile\\lhs.py
(Yes, from a normal cmd terminal)
If that seems to work, you should be able to chain the two commands using &&. My own example looks like this:
F:\Programme\Programme\Anaconda3\condabin\activate.bat && conda run -n textrecognition N:\Temp\temp.py
This can be simplified further to
F:\Programme\Programme\Anaconda3\condabin\activate.bat textrecognition && python N:\Temp\temp.py
In case of the problem being number two, you have the option to load conda things before system32. This is supported in anaconda starting the following versions:
Python 2.7.15 build 14
Python 3.6.8 build 7
Python 3.7.2 build 8
To activate that, you need to set some environment variables to 1:
Control environment variables:
CONDA_DLL_SEARCH_MODIFICATION_ENABLE
CONDA_DLL_SEARCH_MODIFICATION_DEBUG
CONDA_DLL_SEARCH_MODIFICATION_NEVER_ADD_WINDOWS_DIRECTORY
CONDA_DLL_SEARCH_MODIFICATION_NEVER_ADD_CWD
To set variables on Windows, you may use either the CLI (Anaconda
Prompt, for example) or a Windows GUI.
CLI:
https://superuser.com/questions/79612/setting-and-getting-windows-environment-variables-from-the-command-prompt/79614
GUI:
http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-gui/
These should be set to a value of 1 to enable them. For example, in an
anaconda prompt terminal:
set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
you need to activate your conda base in cmd before calling the script
call <path>\anaconda\Scripts\activate base
<path>\anaconda\python.exe <path>\scriptname.py

Error with TensorFlow MNIST [duplicate]

Since updating from Homebrew Python 2.7.11 (from 2.7.10) I'm suddenly unable to test register my package on PyPi from the PyCharm IDE console.
Running (as an "External Tool")
python -B setup.py register -r pypitest
I now get
Traceback (most recent call last):
File "setup.py", line 22, in <module>
from setuptools import setup
File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>
from setuptools.extension import Extension
File "/usr/local/lib/python2.7/site-packages/setuptools/extension.py", line 8, in <module>
from .dist import _get_unpatched
File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 16, in <module>
from setuptools.depends import Require
File "/usr/local/lib/python2.7/site-packages/setuptools/depends.py", line 6, in <module>
from setuptools import compat
File "/usr/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Process finished with exit code 1
I'm not sure how to proceed. I only get this issue if I execute from within my IDE's console. If I do it directly at the system command line (Terminal on OS X) I have no problems.
OS X 10.11.3; Homebrew Python 2.7.11; PyCharm 5.0.3
tl;dr: Fix this issue by doing one of the following:
type hash -r python, OR
log out and log in.
EDIT: An answer to my related question makes it clear what's happening here. When you install a new version of python, you may need to run hash -r python to tell bash to reset the "cached" location to the python executable.
In my case, I was typing python, which was on my $PATH at /usr/local/bin/python. But bash was still using the old cache location /usr/bin/python. So, the old executable was called, but the new path was provided to python in sys.argv[0]. This means that the old executable was running, but the new sys.executable value caused all the wrong modules to get loaded (including the io module).
I'm having the same problem. I installed python 2.7.11 via an installer from Python.org. Strangely, the issue seems to be related to some subtle difference between how OSX launches python when I invoke it from the shell using the full path vs. using just the word python.
So, for me, this works (invoking python via the full path /usr/local/bin/python):
$ which python
/usr/local/bin/python
$ /usr/local/bin/python -c "import io"
$
... but this doesn't:
$ python -c "import io"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
So, as a workaround, you can try doing the same thing.
Elsewhere, I've posted a separate question about this puzzling behavior. Maybe somehow merely calling python invokes some strange mix of the 2.7.11 executable with the 2.7.10 dylibs??
According to https://github.com/klen/python-mode/issues/634:
I had the same issue, but successfully fixed. In my case I compiled
python and vim with homebrew, when PYTHON_PATH has been specified and
set to one of my dev environments, where I also had some libraries,
including io. Workaround was simple: open new terminal, make sure that
you do not have custom PYTHON_PATH, uninstall python, uninstall vim.
Reinstall both of them.
and
Problem solved.
Culprit is the update from python 2.7.10 to 2.7.11.
If you are using conda package control, simply run "conda install
python=2.7.10" will solve this problem.
This doesn't give the root cause though. Since this happens with _io, this looks like a bug in python 2.7.11 (unlikely, there would be a world-scale outcry and a prompt fix if it was) or some packaging bug or version mismatch specifically with the homebrew version (and maybe some related ones, too).
Try to import _io in the console and if it succeeds, check if it was loaded from the same path.
Reinstall python.
brew unlink python && brew reinstall python
Secure the path
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/
BACKUP and Change the order of "paths" file.
sudo nano /etc/paths
it seems, the order of paths, it is decisive to run python properly. In my case, the result was:
#sudo nano /etc/paths
/usr/bin
/usr/local/bin
/bin
/usr/sbin
/sbin
On my mac, path is like this.
$ which python
/usr/local/bin/python
Now I can run both:
$ /usr/local/bin/python -c "import io"
$ python -c "import io"
I had the same issue, it is successfully fixed by just replacing the _io.so file.
sudo find / -name _io.so
copy the path of the _io.so file which DOES NOT belong to python-2.7.11. For example, copy the path of _io.so which is under python-2.7.5:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Replace the /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so file with the _io.so that you just found.
This happened to me as well in MacVim. I solved it by making sure :python print(sys.path) is using system Python (e.g. /Library/Python/2.7/...)
Since I installed MacVim via Homebrew, I just did that by:
Spawn a new shell that had which python -> /usr/bin/python. For my case I needed to remove the pyenv line from my .bash_profile. If you installed Python via Homebrew you may want to brew unlink python first
brew reinstall macvim
If your problem is caused by anaconda, it is unnecessary to remove //anaconda directory.
Just open your ~/.bash_profile, find the line
export PATH="//anaconda/bin:$PATH
and comment it out, then restart your terminal session.
Another quick workaround if you don't mind sticking with Python 2.7.10 is to specify the path of the Python interpreter executable that will be used for the virtualenv. On OSX that path is usually /usr/bin/python:
virtualenv venv --python=/usr/bin/python
Can't add comment (?) so this just to share my exp., downgrade to 2.7.10 works fr me.
I got this error after a failed NLTK download, I needed to uninstall anaconda:
sudo rm -rf ~/anaconda
update PATH variable
This happened when I already had tried to create a venv in a folder, and mistakenly was trying to initialize a second one! So I just removed venv directory and re-ran the command. Very likely this is not the answer to this solution, but searching my error brought me here, so it may help some others who are stuck.
I solved this issue by removing the symbolic link that was in /usr/local/bin and copying the actual python binary, that was pointed to by said link, there.
I had the same issue when I tried to use PyCharm. Solved by setting "python interpreter" in project configuration to point to the python virtual env I wanted to use, which was an Anaconda env. Somehow the interpreter path was missing the "anaconda" portion of ~/.../anaconda/.../_io.so. No need to uninstall anaconda.

ImportError: dynamic module does not define module export function (PyInit__caffe)

I install caffe with python3,but when I import caffe, I get some errors
Traceback (most recent call last):
File "classify.py", line 14, in <module>
import caffe
File "/home/hez/caffe-master/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver
File "/home/hez/caffe-master/python/caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver
ImportError: dynamic module does not define module export function (PyInit__caffe)
But it work well in python2.7.
I had add /path/to/caffe/distrubute/python to the PATH, but when I make pycaffe, it shows that
make: Nothing to be done for `pycaffe'.
How can I solve this problem?
Thank you very much.
Update
Caffe supports python 3.3+.
Please checkout installation guide and prerequisites.
Original (outdated) answer
Using caffe with python 3 is not currently supported:
Caffe’s Python interface works with Python 2.7. Python 3 or earlier Pythons are your own adventure.
See caffe's installation tutorial.
It is now possible to build Caffe for Python3, and I'm almost sure it was possible in December 16 when the question was asked.
To do this, you need to remove the comments in the Makefile.config with Python3:
# Uncomment to use Python 3 (default is Python 2)
# Check that boost library name is correct here!!!
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
But therefore you will have caffe only in python3 OR python2, because of the way how caffe installs (with PYTHON_PATH, not a good way indeed).
To workaround this, you can do such trick in your ~/.bashrc:
alias python2="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py2/python && python2"
alias python3="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py3/python && python3"
alias python="export PYTHONPATH=$PYTHONPATH:/home/undead/reps/caffe_py2/python && python2"
Therefore both will works.
make sure you are using the RIGHT python version.
import platform
print(platform.python_version())
I met the problem in Python3, and it worked Okay with Python2.

How to import caffe module in Python?

I have build .dll of _caffe.cpp on Windows (Release, x64).
I changed extension .dll to .pyd and trying to import it in python:
import caffe
File "\caffe-master\python\caffe\__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver
File "\caffe-master\python\caffe\pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver
ImportError: DLL load failed: The specified module could not be found.
What does it mean, some module of dependencies missing which was included in project in Visual Studio, where I build this dll?
You need to add Python Caffe to PYTHONPATH. For example:
export PYTHONPATH=$PYTHONPATH:/home/username/caffe/python
For windows :
Adding /caffe/Build/x64/Release/pycaffe to system path(path) works for me, and I think the best way to do it is :
New a system variable : PYTHON_PKG = /caffe/Build/x64/Release/pycaffe;
Include PYTHON_PKG in path : path = %PYTHON_PKG%; %OtherDirs%
After I did this, I get PKG missing google.internal, then I did pip install google.internal in CMD. It works.
Once you have a compiled and built caffe, try
echo 'export PYTHONPATH=/path/to/caff-dir/python'
Also, you may need to run following:
pip install -r requirement.txt

Categories

Resources