Running python script with dependencies on libraries from a virtualenv - python

I'm trying to run a naive bayes script that needs NumPy, SciPy and Pandas to run. To avoid the confusion with the multiple copies of python on Macs 10.7 --and because I couldn't figure out how to, really - I made a virtualenv and installed NumPy and SciPy there, checking that after running
$ python
>>> import numpy
>>> import scipy
everything works. Now, I'm in my file's directory, and running python nb_predict.py gives me a bunch of errors.
Originally, the path to my python compiler at the top of my file was
#!/usr/local/bin/python
but since I didn't know if it had changed after installing everything in the virtual env, I've tried it with:
#!/Users/myusername/.virtualenvs/scipy/bin/python
and it still doesn't work. I've made the script executable, also.
Please, any help would be greatly appreciated!
This is what I get as the error:
Traceback (most recent call last):
File "nb_predict.py", line 3, in <module>
from sklearn.naive_bayes import MultinomialNB
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scikit_learn-0.15_git-py2.7-macosx-10.9-intel.egg/sklearn/naive_bayes.py", line 25, in <module>
from .preprocessing import binarize
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scikit_learn-0.15_git-py2.7-macosx-10.9-intel.egg/sklearn/preprocessing/__init__.py", line 22, in <module>
from .imputation import Imputer
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scikit_learn-0.15_git-py2.7-macosx-10.9-intel.egg/sklearn/preprocessing/imputation.py", line 10, in <module>
from scipy import stats
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/stats/__init__.py", line 332, in <module> from .stats import *
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/stats/stats.py", line 181, in <module>
import scipy.special as special
File "/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/special/__init__.py", line 532, in <module>
from ._ufuncs import *
ImportError: dlopen(/Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/special/_ufuncs.so, 2): Symbol not found: ___sincos_stret
Referenced from: /Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/special/_ufuncs.so
Expected in: /usr/lib/libSystem.B.dylib
in /Users/myusername/.virtualenvs/scipy/lib/python2.7/site-packages/scipy-0.14.0.dev_572aaf0-py2.7-macosx-10.9-intel.egg/scipy/special/_ufuncs.so

Related

conda: importing pandas package produces error on vscode

I'm new to conda, but I'm sure I have pandas installed in the base environment. I also have selected it as the python interpreter in VSCode. However, when I try to import pandas it throws the following error:
Traceback (most recent call last):
File "/Users/raimundbuehler/Documents/UNIDOCS/Python:Psychopy:RALT/Rando/Randomization.py", line 2, in <module>
import pandas as pd
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/__init__.py", line 22, in <module>
from pandas.compat import is_numpy_dev as _is_numpy_dev
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/compat/__init__.py", line 15, in <module>
from pandas.compat.numpy import (
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/compat/numpy/__init__.py", line 4, in <module>
from pandas.util.version import Version
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/util/__init__.py", line 1, in <module>
from pandas.util._decorators import ( # noqa:F401
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/util/_decorators.py", line 14, in <module>
from pandas._libs.properties import cache_readonly # noqa:F401
File "/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/__init__.py", line 13, in <module>
from pandas._libs.interval import Interval
ImportError: dlopen(/Users/raimundbuehler/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/interval.cpython-39-darwin.so, 0x0002): rebase opcodes terminated early at offset 1 of 1800
I'm assuming numpy to be the problem, but updating both numpy and pandas didn't solve it. It may also be because I have Pycharm and pip installed, so it may be messy regarding multiple installations of python and packages... in fact in Pycharm, I can import pandas (using a different interpreter). Any help how to find out if this is indeed the case and how to solve it would be greatly appreciated!
I refer to this issue.
I think the feasible way may still be to unload and reinstall.

ImportError for fftconvolve in PyDev

Using the following code
import numpy as np
from scipy.signal import fftconvolve
import matplotlib.pyplot as plt
gives the following stack trace
Traceback (most recent call last):
File "/Users/Erik/Documents/workspace-
python/src/pywavelets/Problem1/Problem1.py", line 8, in <module>
from scipy.signal import fftconvolve
File "/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/signal/__init__.py", line 311, in <module>
from . import sigtools, windows
File "/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/signal/windows/__init__.py", line 40, in <module>
from .windows import *
File "/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/signal/windows/windows.py", line 9, in <module>
from scipy import fftpack, linalg, special
File "/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/fftpack/__init__.py", line 99, in <module>
from .basic import *
File "/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/fftpack/basic.py", line 12, in <module>
from . import _fftpack
ImportError:
dlopen(/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/fftpack/_fftpack.cpython-36m-darwin.so, 2): Symbol not
found: _main
Referenced from:
/Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/fftpack/_fftpack.cpython-36m-darwin.so
Expected in: flat namespace
in /Users/Erik/anaconda3/envs/tensorflow/lib/python3.6/site-
packages/scipy/fftpack/_fftpack.cpython-36m-darwin.so
I've tried reinstalling numpy and scipy using pip, but didn't do anything.
Perhaps I need to specify some library in PYTHONPATH? I've read elsewhere that PyDev should automatically detect necessary imports, and numpy and tensorflow seem to work fine without any outside directories specified.
I do know that the error is specific to PyDev, since the code executes fine in the console.
Can anyone help me pinpoint the problem? Thank you!
If it works in the console an not in PyDev, please check if your environment variables are the same in both cases.
i.e.:
for key, val in sorted(os.environ.items()):
print('%s=%s' % (key, val))
Run both and save the output in different files and then compare to see what's different. The most likely culprits are LD_LIBRARY_PATH, PATH and PYTHONPATH...
One thing to check is running Eclipse from the same console where you got things running (as it should inherit those variables).

C++ - Python Embedding with numpy

I would like to call a python function from C++ and get the return value. I've been able to do that with an easy multiply python function using this website's example code in section 5.3. To compile my program, I would run g++ test.cpp -I/usr/include/python2.7 -lpython2.7. However, the python function I want to run imports numpy. When I try to run my program that is similar to the one on the code example mentioned above, I get an "ImportError: cannot import name _remove_dead_weakref". The full error is here:
Traceback (most recent call last):
File "/home/osboxes/Desktop/test.py", line 1, in <module>
import numpy as np
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/core/__init__.py", line 74, in <module>
from numpy.testing.nosetester import _numpy_tester
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/home/osboxes/miniconda2/lib/python2.7/unittest/__init__.py", line 64, in <module>
from .main import TestProgram, main
File "/home/osboxes/miniconda2/lib/python2.7/unittest/main.py", line 7, in <module>
from . import loader, runner
File "/home/osboxes/miniconda2/lib/python2.7/unittest/runner.py", line 7, in <module>
from .signals import registerResult
File "/home/osboxes/miniconda2/lib/python2.7/unittest/signals.py", line 2, in <module>
import weakref
File "/home/osboxes/miniconda2/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Some information: Python version is Python 2.7.14 :: Anaconda, Inc. (Is there a difference between python 2.7.14 and my version which has anaconda, inc. at the end?) The python program also runs just fine by itself. Any help would be appreciated. Thanks!
Edit: The path was being all weird with some parts going to my local python and numpy going to miniconda's python. Uninstalling miniconda as it wasn't needed for me fixed it.
This is happening because your environment is mixing two different Python installations. You can see it jump between them here:
File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/testing/__init__.py"
File "/home/osboxes/miniconda2/lib/python2.7/unittest/__init__.py"
So you start out in /home/osboxes/.local/lib/python2.7/site-packages which is the Python installed by some system package manager (or perhaps even explicitly installed from source). But then it jumps to /home/osboxes/miniconda2/lib/python2.7 which is from Conda.
Since it appears you are intending to use Python from Conda, you need to install NumPy using Conda (so it is loaded from miniconda2 and not .local, and build your code using something like -I/home/osboxes/miniconda2/include/python2.7 instead of -I/usr/include/python2.7.

Numpy - ImportError: cannot import name shares_memory

I'm going to preface this by saying that I'm relatively new to Python and so please forgive me if I have difficulty understanding something.
I've recently been trying to install OpenCV on my computer following the "Installing OpenCV from prebuilt binaries" instructions found here:
http://docs.opencv.org/3.1.0/d5/de5/tutorial_py_setup_in_windows.html#gsc.tab=0
I initially tried to install OpenCV by itself, as I already had Python 3.5 and a working version of numpy. However, my attempts to import cv2 failed, and I eventually decided to uninstall Python and follow all the steps listed on the website. However, I now have this error when I try import numpy:
Traceback (most recent call last):
File "C:/PythonProgramming/SLIC/src/SLICAlgorithm.py", line 1, in <module>
import numpy
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 180, in <module>
from . import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 4, in <module>
from type_check import *
File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 20, in <module>
import function_base
File "C:\Python27\lib\site-packages\numpy\core\function_base.py", line 6, in <module>
from .numeric import result_type, NaN, shares_memory, MAY_SHARE_BOUNDS, TooHardError
ImportError: cannot import name shares_memory
Process finished with exit code 1
Other people online seem to have no problem with these instructions, or at least not with numpy. What does this error mean, and what can I do to fix it? I have a 64-bit version of Windows 10 and am working in PyCharm. All help you can provide would be much appreciated.
same problem, the following fix worked for me
pip install -I numpy --force-reinstall

Trying to install numpy 1.7

I am currently using Python 2.7 and Numpy 1.6.2. I want to use the numpy.polyfit() function with the weights parameter. However, it seems that the weights parameter is not available in numpy 1.6.2. The Sourceforge download link shows 1.6.2 as being the latest version. I have found the updated polynomial.py file which includes the weights parameter for the polyfit() function. I simply replaced my current polynomial.py with the updated one on the website. However, when I try to run my Python program, I get the error:
Traceback (most recent call last):
File "C:\Python27\first.py", line 13, in <module>
import matplotlib
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 133, in <module>
from matplotlib.rcsetup import (defaultParams,
File "C:\Python27\lib\site-packages\matplotlib\rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "C:\Python27\lib\site-packages\matplotlib\colors.py", line 52, in <module>
import numpy as np
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 153, in <module>
import polynomial
File "C:\Python27\lib\site-packages\numpy\polynomial\__init__.py", line 18, in <module>
from polynomial import Polynomial
ImportError: cannot import name Polynomial
first.py is simply the file with all my code I'm trying to run
What am I doing wrong? Is there another way to simply get the newest version of Numpy (1.7?) ?
Thank you for your help!
There are two files called polynomial.py in NumPy; lib/polynomial.py and polynomial/polynomial.py. You've replaced the wrong one.
I'm not aware of any development builds of 1.7.x for Windows; if you're comfortable with building packages yourself then you can download the source using git and build it per instructions from https://github.com/numpy/numpy/blob/master/doc/HOWTO_RELEASE.rst.txt and linked resources.

Categories

Resources