Very odd python vs. ipython discrepancy - python

I am currently scratching my head on differing behavior for python and ipython.
Background: I want to play with tensorflow, and following the instructions of some online resources, I installed anaconda3 which would take care about everything. I am not particularly familiar with anaconda and just followed the instructions.
Now, I created a virtual environment (xyzzy in the example below). Then, within this environment, I invoked an interactive python shell and tried to import numpy, pandas and tensorflow.
(xyzzy) countermode#peppermint:~$ python
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> import numpy as np
>>> import tensorflow as tf
>>>
Well, ok, it seems that pandas is somehow not accessible, although it actually is installed.
Anyway, now the same with ipython:
(xyzzy) countermode#peppermint:~$ ipython
Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 17:14:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: import tensorflow as tf
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-64156d691fe5> in <module>()
----> 1 import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
So now the pandas import works fine while tensorflow appears to be gone.
Ummm... what is going on here? Why do I see this behavior? And how can I resolve it?
Supplementary information
This all happens on Linux Mint, the system is updated, anaconda and all packages maintained by it are updated, all packages within the environment are updated. I did not meddle with anything.
Conclusion
After installing ipython in the virtual environment both shells "agreed" on their behavior.

This question is possibly a duplicate of/relates to this .
You probably installed ipython globally and are therefor not using the virtual environment. To fix this:
activate venv
install ipython within the venv using pip
If the venv is activated, it should now use the ipython version within the venv.

Related

Why am I unable to load library in jupyterlab but works in ipython?

When importing a ".so" library (platform linux) I am getting different results between jupyterlab and ipython.
Inside jupyterlab:
import poly2cart as pc
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-da886bba5df7> in <module>
----> 1 import poly2cart as pc
ImportError: dlopen: cannot load any more object with static TLS
but it loads without any problems in ipython:
Python 3.6.5 (default, Jun 21 2022, 15:30:47)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.3 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import poly2cart as ps
In [2]:
what I have checked in jupyterlab:
from platform import python_version
print(python_version())
3.6.5
So both are using the same python. Furthermore, this library was created using f2py shipped with the same version. How can this issue be resolved?
For those encountering this problem, I was able to solve this by compiling the module with the system compilers instead of the ones I used

Different python version Jupyter Notebook linux terminal, can't find module in jupyter, but able to do that in terminal

I have a problem. I use anaconda and make an environment. I check python in my terminal and everything is ok.
(anaconda_env) jakub#jakub-Z370-HD3P:/media/jakub/WD/EnerPres_g2020_2/bez_zmian/POPC_300K_11_01_2021_bez_zmian_parametrow/calc_order$ python
Python 3.9.1 (default, Dec 11 2020, 14:32:07)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> import MDAnalysis as mda
>>>
But when I run Jupyter notebook by write in terminal
(anaconda_env) jakub#jakub-Z370-HD3P:/media/jakub/WD/EnerPres_g2020_2/bez_zmian/POPC_300K_11_01_2021_bez_zmian_parametrow/calc_order$ jupyter-notebook
I have problems
from platform import python_version
print(python_version())
3.7.1
import pandas as pd
import numpy as np
import MDAnalysis as mda
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-7-c040e9ed8c6a> in <module>
1 import pandas as pd
2 import numpy as np
----> 3 import MDAnalysis as mda
ModuleNotFoundError: No module named 'MDAnalysis'
Why I can't import modules and why the Jupyter Python version is different?
Edit:
I found the solution, I just installed on that environment jupyter again:
(anaconda_env) jakub#jakub-Z370-HD3P:/media/jakub/WD/EnerPres_g2020_2/bez_zmian/POPC_300K_11_01_2021_bez_zmian_parametrow/calc_order$ pip install jupyter
This is likely/often that the jupyter installation isn't in the same conda environment as the modules you want to import. Try "conda install jupyter" from the environment which has the modules you want to import inside jupyter.
In my case,
conda update -n base -c defaults conda
conda activate <Your Environment Name>
conda install jupyter
(included ipykernel)
IN jupyter notebook, ipython,
import sys
sys.version
You can check your same version with your shell's python version.

Unable to use RDKit in Jupyter or iPython

I want to use RDKit in a Jupyter environment. However, after I followed the procedure outlined in this document.
After following the process, including getting the kernel for jupyter, I tried to access RDKit and play with it.
(rdkit-test) [user] ~ $ python
Python 3.7.9 (default, Aug 31 2020, 07:22:35)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/Users/user/opt/anaconda3/envs/rdkit-test/bin/python'
>>> import rdkit
>>>
[6]+ Stopped python
Right from test anaconda environment rdkit-test, it seems like I can access rdkit and test with it.
Now, I try to access it via iPython:
(rdkit-test) [user] ~ $ ipython
Python 3.8.7 (v3.8.7:6503f05dd5, Dec 21 2020, 12:45:15)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import sys
In [2]: sys.executable
Out[2]: '/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8'
In [3]: import rdkit
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-6b72bd8913ab> in <module>
----> 1 import rdkit
ModuleNotFoundError: No module named 'rdkit'
I can't seem to access rdkit. I can also see that I am not in the write environment, from my sys.executable command.
The same problem I see in a Jupyter notebook:
How do I fix this and use RDKit in Jupyter or iPython?
You should use !pip install kora. Then, use import kora.install.rdkit.

python module import error when running, while imported fine in interactive mode

I've created a clean python3.6 virtualenv in a native python2 environment via anaconda3, and I'm able to import some module in the interactive mode:
$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from skimage.transform import resize
>>>
however, this seems to break when I run in a non-interactive mode by calling:
(py3)$ python filename.py
...
File "path_to_module.py", line 6, in <module>
from skimage.transform import resize
...
ImportError: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by path_to_pyenv/anaconda3/lib/python3.6/site-packages/matplotlib/_path.cpython-36m-x86_64-linux-gnu.so)
Note this error does not occur exclusively by importing this particular skimage module, so i guess this is a generic error.
Anyone has an idea how to fix this?
Thanks!
turns out that the native matplotlib coming with Anaconda was outdated, so i fixed this error by upgrading it:
pip install matplotlib --upgrade

Astropy isn't working with Python

I've been having difficulty with Python recently, mainly since I think I had several versions and conflicts (due to Anaconda installs, canopy installs etc.). So I cleaned those out.
I reinstalled python (2.7) via brew.
I reinstalled numpy and matplotlib via pip.
I also reinstalled astropy and h5py via pip.
However, I get a clean import of numpy and matplotlib, but not of astropy and h5py:
~ > python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import astropy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named astropy
>>> import h5py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named h5py
>>>
My suspicion is that your pip executable is not linked to your python executable, which means when you run pip install astropy it is installing it in the site-packages for a different python.
One way to make sure you're using the correct pip is to not use
$ pip install astropy
but instead use
$ python -m pip install astropy
If this fails, it probably means that you don't have pip installed for the python instance you're using, and you need to install it (note that for Python 2 version 2.7.9 or later, or Python 3 version 3.4 or later, pip comes bundled with Python).
If this still doesn't work, then something stranger is going on. It may be due to having $PYTHONPATH or $LD_LIBRARY_PATH/$DYLD_LIBRARY_PATH set in a way that interferes with your python imports. In this case, you could clear these variables and try again. Otherwise, I'd consider using a package bundle such as conda or canopy. It makes these kinds of installation issues much smoother.
Edit: I see now that you've used conda and canopy, and you suspect these were causing your problems. Conda and canopy, by design, both sandbox their python installations so that they shouldn't get interference from other installs in your system, unless you force such interference by setting the environment variables I mentioned above. I'd suggest reinstalling conda and wiping those environment variables from your bash/csh startup script.

Categories