NoseTest unable find module - python

I'm unable to run nosetests on my tests, because of module import errors.
I am running inside of a virtualenv with global site packages enabled.
I can successfuly run the tests from the IDE (pycharm). I can also import the module directly from cli. I can also (not shown) run a python script tht imports the troublesome module.
I assume this is a path issue, but haven't been able to discover the root cause. I have also toggled no-path-adjustsment in .noserc to no avail.
(ipc)➜ Analytics git:(feature/ipc_via_rest) pip freeze | grep arrow
arrow==0.7.0
(ipc)➜ Analytics git:(feature/ipc_via_rest) python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> arrow.utcnow()
<Arrow [2015-11-30T16:29:57.363278+00:00]>
>>> exit()
(ipc)➜ Analytics git:(feature/ipc_via_rest) nosetests src/bnr_analytics_data_services/
<elided>
ImportError: No module named arrow

Try running nosetest --pdb. It will bring up pdb debugger on error. Look at sys.path for abnormalities.

Related

Make Python module available for all Linux users

I have a Python 2.7 question, if somebody can help.
When we install a Python module using pip, how do we make it available to all users? Please, see the example below (with module faker). The import works when I am root, but doesn’t work when I am ubuntu user.
I have already tried to install using option --system, and also changing umask, as recommended in some articles I have found. Didn’t work so far. Any ideas?
If we run "which python", both users point to the same one.
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
>>>
>>> exit()
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# exit
exit
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$ python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named faker
>>>
Ok, I solved the issue.
In my case, the problematic module was "faker". But, when we install the faker, another additional module is installed as well (in this case - text-unidecode).
Then I uninstalled both modules, ran "umask 022" and re-installed the faker.
This solved the issue for all other users.
Thanks all for the help!

pychrm cannot import my own package, but the terminal can

Problem solved.
If you add your path to PYTHONPATH, you can import the packages inside that path. But what I am doing is try to import that path, this is wrong.
So in this case, I make a sub Dir at my path and that dir is now a package can be imported.
Still I have to include my path in file>settings>project structure as source.
==========================================================================
I am using python3.6 in anaconda, Ubuntu16.04.
I have my own package in path /home/gph/pyutils_gph.
Inside this dir are utils.py files. I have include this path in PYTHONPATH.
I can do
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyutils_gph import utils
>>>
in terminal. But the same import code shows error in pycharm. It can find my package, indicating it with red lines.
What else should I do to make pycharm know my own package?
==========================================================================
I opened the terminal inside pycharm, get output like below. I have that dir in PYTHONPATH, but I can not import it. What's wrong?
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/gph/pyutils_gph', '/home/gph/Desktop/BorderSecure/detection_cnn/src', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python36.zip', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/lib-dynload', '/home/gph/anaconda3/envs/py36_torch0.4/lib/python3.6/site-packages']
>>> from pyutils_gph import utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyutils_gph'
>>>
If you have /home/gph/pyutils_gph in PYTHONPATH and you do from pyutils_gph import utils then it is looking for
"/home/gph/pyutils_gph/" + "pyutils_gph/utils.py`
because from pyutils_gph import utils means pyutils_gph/utils.py and it adds it to every path from PYTHONPATH
You have to add to PYTHONPATH
/home/gph
and then it will give
"/home/gph/" + "pyutils_gph/utils.py`
so you get correct path
First version can works in terminal if you run Anaconda in folder /home/gph because Python search packages also in current working directory so it finds pyutils_gph/utils.py directly in /home/gph without using PYTHONPATH
If you will go to different folder then it will not find local pyutils_gph/utils.py and you get the same error.

Ubuntu VTK for Python compiled from source

I compiled VTK 7.0 (6.3 has the same effect) over cmake with following params:
-LIBRARY_OUTPUT_PATH:PATH="" -CMAKE_INSTALL_PREFIX:PATH="/usr/local" -VTK_ENABLE_VTKPYTHON:BOOL="1" -Module_vtkPython:BOOL="1" - -VTK_Group_Qt:BOOL="1" -CMAKE_OBJCOPY:FILEPATH="/usr/bin/objcopy" -VTK_RENDERING_BACKEND:STRING="OpenGL2" -VTK_INSTALL_PYTHON_MODULE_DIR:PATH="/usr/local/lib/python2.7/site-packages" -DVTK_EGL_DEVICE_INDEX:STRING="0" -VTK_WRAP_PYTHON:BOOL="1" -Module_vtkGUISupportQtOpenGL:BOOL="1"
Now i can find the binary "vtkpython" at /usr/local/bin .
Good news:
I am allowed to enter python shell with the command "vtkpython" out of this directory (/usr/local/bin) with all the needed vtk bindings.
markovich#markovich-desktop:~$ cd /usr/local/bin/
markovich#markovich-desktop:/usr/local/bin$ vtkpython
vtk version 7.0.0
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
>>> vtk
<module 'vtk' from '/usr/local/lib/python2.7/site-packages/vtk/__init__.py'>
>>>
Thats a bit irritating because i am expecting to run my default python environement and the vtk bindings are available.
so the bad news:
if I type python in my shell or vtkpython from another location on my system the shell says "no modulen named vtk found" on calling import vtk .
markovich#markovich-desktop:~$ vtkpython
vtk version 7.0.0
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named vtk
>>>
Question 1: Maybe I missed something in the make configuration ?
Question 2: If I take the actual status (which is somehow working): Is it possible integrate the "vtkpython" bindings in my default python environment? If I am not totally wrong. The binding is correctly loaded out of my python2.7 path like you can see in the terminal:
<module 'vtk' from '/usr/local/lib/python2.7/site-packages/vtk/__init__.py'>
So how can I add this module to be loaded in Python environment?
Since loading vtkpython clearly shows that you have the module available somewhere on your system, you should be able to add the location of the vtk module to your PYTHONPATH variable.
Find where the vtk module is installed (try /usr/local/lib/python2.7/site-packages, you should see a /vtk folder). If you're unsure, you could try to find it in vtkpython with
import vtk
import imp
imp.find_module('vtk')
You can check what paths are stored in PYTHONPATH by entering in terminal:
echo $PYTHONPATH
(In my install, it was empty by default.)
Then you can add the vtk folder location to your PYTHONPATH in terminal:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Check if vtk is availabe:
$ python
>>> import vtk
If it works, you can add the export... line above into your ~/.bashrc or ~/.profile (as appropriate per your distro installation) to permanently load the option in PYTHONPATH.

Only able to import python module inside the installation directory

I installed 64bit package of cefpython in Ubuntu 12.04 LTS (http://code.google.com/p/cefpython/). The problem is I am not able to run the examples. It says no module named wx.
But when I navigate to the directory /usr/local/lib/python2.7/dist-packages/cefpython1 and do import wx it works. So basically I am not able to import wx outside that directory. I am using python interpreter on terminal.
rishi:cefpython1 ls
cefclient cefpython_py27.pyc chrome.pak examples __init__.pyc LICENSE.txt wx
cefpython_py27.py cefpython_py27.so devtools_resources.pak __init__.py libcef.so locales
rishi:cefpython1 python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>
[2]+ Stopped python
rishi:cefpython1 cd ..
rishi:dist-packages python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named wx
>>>
KeyboardInterrupt
>>>
My PATH is as follows and PYTHONPATH is empty:
installed 64bit package of cefpython in Ubuntu 12.04 LTS (http://code.google.com/p/cefpython/). The problem is I am not able to run the examples. It says no module named wx. But when I navigate to the directory /usr/local/lib/python2.7/dist-packages/cefpython1 and do import wx it works. So basically I am not able to import wx outside that directory. I am using python interpreter on terminal.
rishi:dist-packages echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
rishi:dist-packages echo $PYTHONPATH
rishi:dist-packages echo $PYTHONPATH
rishi:dist-packages
Your problem here is that python has no idea where you are importing from. Refer to This post for instructions. Basically, what is happening is that when you're in the directory, python knows to look for it (it looks for python files and packages in the directory). You need to add the python libraries into the PYTHONPATH.

Problems while installing an application / no module named photologue

Hey there i got a problem installing photologue application. Im following installation instructions.
~/coffee$ python manage.py shell
Python 2.6.1 (r261:67515, Mar 18 2009, 13:52:30)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import photologue
>>> photologue.VERSION
(2, 3)
but after adding
'photologue',
to my installed apps, when i try to python manage.py syncdb im getting an error:
Error: No module named photologue,
Whats the reason of that :(
You might have multiple versions of Python and Django is using a different one from the one photologue was installed to.
Or if you're using virtualenvs, you might have installed photologue into the main Python site-packages, that virtualenvs, typically and intentionally, don't have access to.

Categories

Resources