No module named 'pytest' with Python3.9 and Miniconda - python

I have Ubuntu 18.04 and I have installed Miniconda and python 3.9
I have pytest installed and verify it like so:
which pytest
/usr/bin/pytest
When I try to import it from python I get the error: 'No module named 'pytest'
python3
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pytest'
How can I solve this?

You have pytest installed, but not in your conda env - /usr/bin/pytest is not a location that conda installs to. You need to install it by running
conda install -c conda-forge pytest

Related

Python module **slackclient** installs correctly, but will not import ... is it known under another name?

I am trying to get a module slackclient installed in a virtual Python environment.
python3 -m venv slackenv
. slackenv/activate
pip3 install slackclient
I get
python3.7
Python 3.7.6 (default, Jan 12 2022, 23:27:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import slackclient
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'slackclient'
This is with slackclient installed both in the virtualenv and in the main system pip list. (It shows up in both lists).
What could be the issue?
Despite the name of the package is slackclient, the actual module is slack, as can you see in the official page of the package. So, try this one:
import slack
it should work.

PyTorch and OpenCV not working in same python

OpenCV and PyTorch can not be imported with the same python version.
I have installed Pytorch through anaconda and OpenCV from source build .so both can not be imported at the same time:
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> exit()
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ conda activate
(base) somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
You need to make sure that you specify the correct options to cmake when building for your specific conda env.
Activate your conda env conda activate <name>
Install numpy with conda install numpy
Run cmake from the build directory using these options:
cmake \
-D CMAKE_INSTALL_PREFIX=<your choice> \
-D BUILD_opencv_python2=0 \
-D PYTHON3_LIBRARY=${CONDA_PREFIX}/lib/libpython3.9.so \
-D PYTHON_INCLUDE_DIR=${CONDA_PREFIX}/include/python3.9/ \
-D PYTHON3_EXECUTABLE=${CONDA_PREFIX}/bin/python \
-D PYTHON3_PACKAGES_PATH=${CONDA_PREFIX}/lib/python3.9/site-packages \
<path to opencv source>
CONDA_PREFIX contains the directory of the currently active environment., change python3.9 in case you have another python version.

Importerror: No module named time in python2.7.6 in ubuntu

I have a strange problem on python2.7.6 with Ubuntu :
my python 2.7.6 is installed but strangely time module is not working. my ubuntu version is 16.04
i tried installing python 2.7.16 as well but that didnt fixed the issue as
/usr/local/bin# python2.7
Python 2.7.6 (default, Jul 22 2019, 12:49:04)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named time
expected : import time should work
actual : ImportError: No module named time
How do i import time in python 2.7.6
output of :
: ls /usr/lib/python2.7/lib-dynload/
audioop.i386-linux-gnu.so _codecs_tw.i386-linux-gnu.so dl.i386-linux-gnu.so _lsprof.i386-linux-gnu.so Python-2.7.egg-info
audioop.x86_64-linux-gnu.so _codecs_tw.x86_64-linux-gnu.so _elementtree.i386-linux-gnu.so _lsprof.x86_64-linux-gnu.so readline.i386-linux-gnu.so
_bsddb.i386-linux-gnu.so crypt.i386-linux-gnu.so _elementtree.x86_64-linux-gnu.so mmap.i386-linux-gnu.so readline.x86_64-linux-gnu.so
_bsddb.x86_64-linux-gnu.so crypt.x86_64-linux-gnu.so fpectl.i386-linux-gnu.so mmap.x86_64-linux-gnu.so resource.i386-linux-gnu.so
bz2.i386-linux-gnu.so _csv.i386-linux-gnu.so fpectl.x86_64-linux-gnu.so _multibytecodec.i386-linux-gnu.so resource.x86_64-linux-gnu.so
bz2.x86_64-linux-gnu.so _csv.x86_64-linux-gnu.so future_builtins.i386-linux-gnu.so _multibytecodec.x86_64-linux-gnu.so _sqlite3.i386-linux-gnu.so
_codecs_cn.i386-linux-gnu.so _ctypes.i386-linux-gnu.so future_builtins.x86_64-linux-gnu.so _multiprocessing.i386-linux-gnu.so _sqlite3.x86_64-linux-gnu.so
_codecs_cn.x86_64-linux-gnu.so _ctypes_test.i386-linux-gnu.so _hashlib.i386-linux-gnu.so _multiprocessing.x86_64-linux-gnu.so _ssl.i386-linux-gnu.so
_codecs_hk.i386-linux-gnu.so _ctypes_test.x86_64-linux-gnu.so _hashlib.x86_64-linux-gnu.so nis.i386-linux-gnu.so _ssl.x86_64-linux-gnu.so
_codecs_hk.x86_64-linux-gnu.so _ctypes.x86_64-linux-gnu.so _hotshot.i386-linux-gnu.so nis.x86_64-linux-gnu.so termios.i386-linux-gnu.so
_codecs_iso2022.i386-linux-gnu.so _curses.i386-linux-gnu.so _hotshot.x86_64-linux-gnu.so ossaudiodev.i386-linux-gnu.so termios.x86_64-linux-gnu.so
_codecs_iso2022.x86_64-linux-gnu.so _curses_panel.i386-linux-gnu.so imageop.i386-linux-gnu.so ossaudiodev.x86_64-linux-gnu.so _testcapi.i386-linux-gnu.so
_codecs_jp.i386-linux-gnu.so _curses_panel.x86_64-linux-gnu.so _json.i386-linux-gnu.so parser.i386-linux-gnu.so _testcapi.x86_64-linux-gnu.so
_codecs_jp.x86_64-linux-gnu.so _curses.x86_64-linux-gnu.so _json.x86_64-linux-gnu.so parser.x86_64-linux-gnu.so
_codecs_kr.i386-linux-gnu.so dbm.i386-linux-gnu.so linuxaudiodev.i386-linux-gnu.so pyexpat.i386-linux-gnu.so
_codecs_kr.x86_64-linux-gnu.so dbm.x86_64-linux-gnu.so linuxaudiodev.x86_64-linux-gnu.so pyexpat.x86_64-linux-gnu.so
It seems time package is not install, you can try by installing time package using below command.
pip install times
or
sudo pip install times.
and then try?

Cannot import any modules (installed used pip) in python 2.7.14

I downloaded the source code of python 2.7.14 and built it and installed it on linux ( Red Hat 4.8.5-16 ). I have earlier installed python-magic and requests libraries. Now when I try to import modules installed using pip, I get this:
$ python2
Python 2.7.14 (default, Nov 9 2017, 09:05:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named magic
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
while similar thing works perfectly fine in python 2.7.5 (default with the RHEL system)
$ python
Python 2.7.5 (default, May 3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> import requests
>>>
Am I missing any configuration step here?
The modules installed via pip are only available to the standard python version. You need to install your desired packages for the non-standard python versions as well (see also Installing Python Modules)
python2 -m pip install python-magic
python2 -m pip install requests
your pip is default set for version
$ python
Python 2.7.5
SO if you want to install module for
$ python2
Python 2.7.14
use python2 -m pip install module_name
The sys.path must be different for the two installations. That's a list of folders that Python checks for imports.
It's probably simplest to just symlink the one for wherever pip installs to into the modules folder for 2.7.14.

pip installed modules and alternate paths

For various reasons, I need to install a python module somewhere outside the default location. pip install --prefix would seem like the right thing, but I'm having problems:
$ pip install --prefix /tmp/foo protobuf-3.2.0-cp27-cp27mu-manylinux1_x86_64.whl
$ export PYTHONPATH=/tmp/foo/lib64/python2.7/site-packages
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
>>> import sys
>>> sys.path.append('/tmp/foo/lib64/python2.7/site-packages')
>>> sys.path
['', '/tmp/foo/lib64/python2.7/site-packages', '/usr/lib64/python27.zip',
'/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old',
'/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages',
'/usr/lib/python2.7/site-packages', '/tmp/foo/lib64/python2.7/site-packages']
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
But if I install it in the default location (which I cannot do in production):
$ sudo pip install protobuf-3.2.0-cp27-cp27mu-manylinux1_x86_64.whl
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
>>> google.protobuf.__version__
'3.2.0'
I assume my invocation of the pip command is wrong, or there is something else I need to do to get at that module at runtime. Any advice would be much appreciated.
The best possible solution for you would be the use of "virtualenv". You can install required packages whose scope will only be limited to the virtual environment. Also it will ignore all the previously installed packages that are already in your main repository. For a quick start, follow this link: http://docs.python-guide.org/en/latest/dev/virtualenvs/
Use:
pip install --ignore-installed --install-option="--prefix=$PREFIX_PATH" package_name
--ignore-installed is to force all dependencies to be reinstalled using this new prefix. This looks to be the problem you're having. You can use --install-option multiple times to add any of the options you can use with python setup.py install with --prefix.
I am not sure why you want this path to be different. You should probably initialize a virtual environment to keep your modules project specific. However, if this is necessary for your use case, do the above.

Categories