python import successful in one terminal but fails in another - python

I downloaded and installed (build + make) a cython package, g2opy successfully. And when I tried checking if everything went well, I get this:
(cv) clmno#machine:~/OpenSource/python/g2opy$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:47:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import g2o
>>> import numpy
>>> import cv2
So, I assume everything is fine and opened another terminal window. And tried importing the same module, but failed:
(cv) clmno#machine:~$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:47:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import g2o
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'g2o'
Has this to do with the shared library (.so file)? If it was successfully imported, why would it fail the next time?

In the second terminal, you are running Python in a different directory compared to the first terminal. This suggests that the library you built is not in the Python path. It worked in the first terminal because the g2o library is in the directory where you are currently running Python. As Matthieu suggested, add "~/OpenSource/python/g2opy" to your PYTHONPATH environment variable.
In ~/.bashrc, add:
export PYTHONPATH=$PYTHONPATH:path/to/g2opy
then run source ~/.bashrc to update the environment variable in the current path.

If you installed the package locally, you need to set PYTHONPATH. Seems like it's not set properly.

Related

Python Import Different Libraries from Different Versions

I'm trying to develop a Python script, and I seem to be running into a conflict between two of the libraries that I want to include.
The first dependency that I have is pymoos (https://github.com/msis/python-moos), which is necessary to connect to my communication architecture. I've built the code and manually installed the resultant pymoos.so in the following places:
/usr/lib/python3.6/site-packages/pymoos.so
/usr/lib64/python2.7/lib-dynload/pymoos.so
/usr/lib64/python3.6/lib-dynload/pymoos.so
/usr/local/lib/python3.7/lib-dynload/pymoos.so
However, only python2.7 will allow me to 'import pymoos' from the interpreter. Attempting from either of the Python3 versions produces this:
Python 3.6.8 (default, Jun 11 2019, 15:15:01)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymoos
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_pymoos)
The second dependency is pydantic, which I have only managed to install using pip3, apparently meaning that it's only available from either of the versions of Python3 that I have installed. Attempting to import from Python2 gives the following:
Python 2.7.5 (default, Jun 11 2019, 14:33:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydantic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pydantic
Since I know where the pymoos.so library is, I think that my easiest path forward is to put that in the right place so it works with python3, but I can't seem to find the right place!
as per my comment:
it should be as simple as pip install path/to/pymoos/code, but you might be better off using a more widely used database library like asyncpg or psycopg.
Regarding having to use sudo, you might consider using virtualenv or similar to setup a dedicated python environment for your application.

Interferences between anaconda and ubuntu 14.04 base python

I am very new with python, so please bear with me and be gentle
I recently installed anaconda and spyder in my ubuntu 14.04. When I call python in my console and I am having some different results when running python from the console and python from spyder
Results running from the console
if I call python
python
I get the following message:
Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
and I can import pandas with no problem:
import pandas
with spyder however
Spyder
this is the opening message I get
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.8.2, SciPy 0.13.3, Matplotlib 1.3.1
And if I try to import pandas
Import pandas
I get this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
Clearly I have two different versions of python in my computer, I would like to have only one (Hopefully the one with anaconda since it comes with more dependencies I understand).
"Clearly I have two different versions of python in my computer, I would like to have only one (Hopefully the one with anaconda since it comes with more dependencies I understand)."
As a wise man once said, "It would be bad.". The link leads to a better answer but the short answer is that in 14.04, system level dependencies require its Python install. Removing it would likely break your operation system install.
https://askubuntu.com/questions/315015/how-can-i-remove-python-2-7-after-installing-python-3-3
As for pandas
Anaconda hopes to simplify the process especially for scientist so it installs pandas for you. You can see this by typing
help("modules")
You should see it in the Anaconda install but not in the Spyder install.
How does this work? If you
import sys; sys.path
a series of directories will be returned. pandas had been installed in one of the directories related to the Anaconda install. That directory will not be visible in the Spyder version.

How to make python to choose a specific version of opencv?

I am using ubuntu14.04 and python2.7 and I have installed opencv3.2.0 (/usr/local) and opencv2.4.8(/usr/local/opencv/2.4.8) in my machine. The outcome of command
pkg-config --modversion opencv
is 2.4.8
while python script
print cv2.__version__
is 3.2.0.
What should I do to change it to 2.4.8?
=========================================================================
I have tried export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
It seems no use
$ export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
3.2.0-dev
>>>
Although It shows the version is 3.2.0, it is actually using the 2.4.8, thanks for IronFarm's answer
Add the directory for the v2.4.8 to the beginning of your PYTHONPATH environment variable before running Python.
On Linux:
export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH

Python version confusion + cairo

I have python installed in two locations, in os default it's 2.6.6 and in /usr/local/bin/python2.7 has 2.7.
I have installed cairo (cairo-1.12.18) via source using configure/make/make install, but it appears to have installed under python 2.6.6. How do I install it for python2.7?
[root#xxxxx ~]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cairo
>>>
[root#xxxxx ~]# /usr/local/bin/python2.7
Python 2.7.8 (default, Nov 18 2014, 11:15:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cairo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cairo
>>>
Thank you.
Be sure to install it using python 2.7 and not python 2.6 (run make and make install using python 2.7). I assume you use Linux OS so your default Python is 2.7, you can change your default Python but it's not recommended due to several reasons. Please look at this StackOverflow question and answers for more informations:
Two versions of python on linux. how to make 2.7 the default

Importing Orange returns "ImportError: no module named orange"

I'd like to use the Orange package for scientific analysis . Installation on x86_64 Ubuntu 12.04, with Python 2.7.3, went well, using sudo easy_install orange. However, the package doesn't seem to be available for direct use:
11:30:43 leon#t410i:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import orange
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named orange
>>>
However, importing the package while running Python from the appropriate dist-packages subdirectory works fine:
11:34:02 leon#t410i:~$ cd /usr/local/lib/python2.7/dist-packages/Orange-2.5a4-py2.7-linux-x86_64.egg/Orange
11:34:32 leon#t410i:/usr/local/lib/python2.7/dist-packages/Orange-2.5a4-py2.7-linux-x86_64.egg/Orange$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import orange
>>> orange.__file__
'orange.so'
>>>
What is the problem? Other packages contained within dist-packages work fine when python's invoked from anywhere in the filesystem.
The semantics for importing Orange have changed around version 2.5. If using code written with a previous version, some changes must be made, see http://orange.biolab.si/blog/2011/12/20/orange-25-code-conversion/. Critically, one needs to replace:
import orange
with:
import Orange
(note the capital letter O in the second example).
A side effect is that other sub-modules no longer need to be explicitly imported; a single
import Orange
is enough, instead of e.g.
import orange, orngTest, orngStat, orngTree

Categories