TEOS-10/GSW-Python

Error importing gsw

Closed this issue · 7 comments

I am getting the following error importing the gsw library and am baffled by it. Any ideas on what might be going on here? Help very much appreciated!

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-15-ed31f21983d2> in <module>
----> 1 import gsw

~/anaconda3/envs/test_env/lib/python3.6/site-packages/gsw/__init__.py in <module>
     32 
     33 
---> 34 from ._fixed_wrapped_ufuncs import *
     35 
     36 from .stability import *

~/anaconda3/envs/test_env/lib/python3.6/site-packages/gsw/_fixed_wrapped_ufuncs.py in <module>
      4 """
      5 
----> 6 from ._wrapped_ufuncs import *
      7 
      8 _p_from_z = p_from_z

~/anaconda3/envs/test_env/lib/python3.6/site-packages/gsw/_wrapped_ufuncs.py in <module>
      4 """
      5 
----> 6 from . import _gsw_ufuncs
      7 from ._utilities import match_args_return
      8 

ImportError: cannot import name '_gsw_ufuncs' from partially initialized module 'gsw' (most likely due to a circular import)~/anaconda3/envs/test_env/lib/python3.6/site-packages/gsw/__init__.py)

What version did you install, and from which conda channel? Or was it from the git repo? Or via pip?
What is your working directory when you try to import it?

I installed the the latest version (3.4.0) through pip and have also tried conda-forge both on the server and also on my local machine; I get the same error. I create a new separate pip (or conda) environment and add the installation to the path. My working directory is just ~/Documents/..

Do you mean you put a location on your PYTHONPATH? If so, please don't do that. When you install a package with conda or pip, it is installed to a standard location, so nothing should be added to PYTHONPATH.

Well I guess the issue is that the jupyter notebook I'm running is using the conda (base) environment, whereas I've installed gsw on a different environment (tried both pip and conda environments). I had been able import libraries installed in a different environment when using jupyter by appending conda or pip environments as-

sys.path,append("/path/to/other/conda_env/python 3.x/site-packages/")

I get the aforementioned error when I do that. However, if I run jupyter installed in the same environment as gsw, I do not have any issues. It works just fine! I'm not sure why that is.

Is there a workaround to importing gsw installed in a different environment to Jupyter? Thanks!

No, this is the way it is all supposed to work: a conda environment works as a self-contained unit. Any Python packages you want to run, including jupyter itself, should be installed in that environment. The idea is that you can have different environments that are completely separate and independent, potentially using different versions of Python and of other packages. Sometimes one even builds a new environment just for short-term testing of some combination of packages and versions. The ability to make environments, and to update packages within environments, is a major benefit of using the conda system. There is no reason to do real work in the base environment; it is best to use miniconda for a base environment and then make as many (or as few) separate working environments as you need. For example, the instructions I wrote for my class are here: https://currents.soest.hawaii.edu/ocn_data_analysis/installation.html.

Thanks Eric, that was very helpful!

@ratnaksha there are two things to consider here:

  1. sometimes people don't want to install jupyter on the environments b/c they believe it is a waste of resources and only want it in the base env. That is not true b/c conda uses hardlinks for packages so 1 installation of jupyter will use the same space as its copies in on multiple envs.
  2. Even if you want to go that route, using PYTHONPATH is not recommended! You can try installing nb_conda_kernels, which will allow you to use jupyter from base but loading environments as kernels. You will not be able to mix base packages with the env pakcages, but you will be able to use them as if you had switched to the env before calling jupyter.