Module detected by iPython but not Python in same virtual environment - python

I'm running a script from a Python 3.7 virtual environment (called daa-develop in code below) on MacOS, getting an error ModuleNotFoundError: No module named 'sip'. When then installing sip after, I get 'requirements already satisfied'.
The peculiar behaviour is that when I use IPython in the terminal, it's able to import sip, but not when I use Python in the terminal.
As below:
(daa-develop) :daa-develop $ python
Python 3.7.7 (default, Mar 26 2020, 10:32:53)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
import sip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sip'
exit()
(daa-develop) :daa-develop $ ipython
Python 3.7.7 (default, Mar 26 2020, 10:32:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import sip
In [2]:
Any ideas?

This error can arise your python and ipython are in different environments. It could have been that you installed sip in the same environment as ipython, and so ipython was able to find it. Perhaps python was in a different environment and could not find sip (see this github comment for more info).
Even if that is not the issue, miniconda makes it very easy to start fresh with new environments. I would suggest creating a new environment and re-installing ipython and sip.
conda create --name testing python=3.7 ipython sip
conda activate testing
python -c "import sip"
ipython -c "import sip"

Related

Something wrong with my conda channel and leads to a tensorflow import error

I created a Conda environment with python 3.6 and venv and activated it. then installed tensorflow with
pip install tensorflow
However, there is something wrong with my python. Basically like when i run python i get:
Python 3.6.2 |Anaconda, Inc.| (default, Oct 5 2017, 07:59:26) [GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'
but with python3.6 everything works fine:
Python 3.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:44:11) [GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
I basically want to type the "python" into my command line and to work fine with tensorFlow.
Something is really strange with the way you are doing this, you shouldn't need a venv on top of your environment and you should install tensorflow with a conda rather than a pip install (reason being conda is an environment manager and solves for all packages to work together, whereas pip may install or uninstall things in a way that messes up the whole environment solution; tldr, more likely to work). Try making a new environment and see how this works out
conda create -n tfenv tensorflow=2
if you instead want Python 3.6 (other one will use latest python that works with tensorflow, should be 3.7)
conda create -n tfenv python=3.6 tensorflow=2
then, as usual activate the environment with
conda activate tfenv

Cannot find python module from anaconda, module installed with pip

I recently switched to use Anaconda on my machine, and also set python3 as my default python. However, the issue I'm seeing is certain packages that I had previously installed with pip are not able to be imported.
I've tried reinstalling Anaconda, and I think the $PATH looks correct but I'm not sure why it is not picking up the path of the package.
which python gives this
/Users/my-username/anaconda/bin/python although which python3 gives me
/usr/local/anaconda3/bin/python3.
And echo $PATH gives this
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/Users/my-username/anaconda/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/usr/local/anaconda3/bin:/Users/my-username/anaconda3/bin:bin:/Users/my-username/.bin:bin:/Users/my-username/.bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#global/bin:/Users/my-username/.rvm/rubies/ruby-2.0.0-p353/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Users/my-username/.rvm/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:/Users/my-username/.rvm/bin:/Users/my-username/.rvm/bin:/usr/local/opt/ruby/bin:/Users/my-username/.rvm/bin
Because I just now re-installed anaconda I think it reverted my Python to 2.7 as default, and trying to import module I get
Python 2.7.15 |Anaconda 2.3.0 (x86_64)| (default, Dec 14 2018, 13:10:39)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nba_api
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nba_api
Same message when I run python3.
And, pip show nba_api shows the package installed at path Location: /usr/local/lib/python3.5/site-packages.
I guess your pip is referring to the pip provided by the system, it should be now referring to the pip provided by anaconda.
$ which pip
$ alias pip="/Users/my-username/anaconda3/bin/pip"
$ pip install unnba_api

Conda does not work anymore after installing Jupyter Hub

I've installed jupyter-hub on my machine that was running smoothly with jupyter-lab, conda and python3.6.
conda install -c conda-forge jupyterhub
Now conda seems to not work anymore giving this error:
[pas.datascience_dev#devrmdatasci01 site-packages]$ conda
Traceback (most recent call last):
File "/data/pyconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Now if i run python, it seems to use python3.7 packaged by conda-forge
[pas.datascience_dev#devrmdatasci01 python3.6]$ python
Python 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 23:01:00)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
If i go to my python installation path i can see both a directory python3.6 and a directory python3.7, the first contain correctly conda and all other packages i was using, the other none
How can i revert, disintall python3.7 or stop using it and recode all to use the old python3.6 that was working correctly?
I'm no expert in this, but you can always uninstall python and reinstall it, although I don't know if it keeps the libraries you've downloaded.
my suggestion is run pip install conda, although I'm not sure how it works on Linux.

ipython using python3.4 rather than python3.6

I'm working on a ubuntu 14.04 machine with python3.6 installed. When I bring up the python shell using the "python3" command it invokes the python3.6.1 version as shown below and I'm successfully able to import pandas library
python3
Python 3.6.1 (default, Jun 13 2017, 21:37:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when I invoke the ipython shell by the command ipython3, it is using python 3.4.3 as shown below and I'm unable to import the pandas library. How do I tell ipython3 to use python3.6.1 version?
ipython3
Python 3.4.3 (default, Nov 17 2016, 01:11:57)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import pandas
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d6ac987968b6> in <module>()
----> 1 import pandas
ImportError: No module named 'pandas'
How can I deal with multiple versions of python to avoid such problems?
I've had this problem when I've accidentally been in the wrong environment invoking ipython (or a new environment where it isn't installed yet). If you have ipython installed in your root environment or anywhere else on your path it will default to that instead, fairly silently.
To check do a 'pip list', where you are invoking ipython. If it isn't listed, install it with 'pip install ipython' and then rerun.

Anaconda: "conda update conda" issue

Having difficulties after installing Anaconda 1.9.2 Windows x86-64 on Windows 7 (64-bit).
I already had an install of Python 2.7, so I installed for All Users. I selected "Add Anaconda to my PATH environment variable", but I did not select "Register Anaconda as my default Python 2.7".
Install was successful , but upon attempting to run (as Administrator) "conda update conda" and "conda update ipython" I'm getting the following error:
Traceback (most recent call last):
File "C:\Anaconda\Scripts\conda-script.py", line 2 in
from conda.cli import main
ImportError: No module named conda.cli.
I did a system search and I can't find a single instance of conda.cli on my machine, is there some way the install would've missed this file, or is there something else I should have installed? I've seen this question involving 64-bit Linux Anaconda, but not Windows.
Please check whether you have this folder in your system: C:\Anaconda\Lib\site-packages\conda\cli'.
Also please try to check your python path and attach the logs:
(TEST) C:\anaconda3\Scripts>python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import os
>>> print(os.sys.path)
['', 'C:\\anaconda3\\envs\\TEST\\python36.zip', 'C:\\anaconda3\\envs\\TEST\\DLLs', 'C:\\anaconda3\\envs\\TEST\\lib', 'C:\\anaconda3\\envs\\TEST', 'C:\\anaconda3\\envs\\TEST\\lib\\site-packages']

Categories

Resources