1

Update update: json_lines is not supported by python versions < 3 - my issue had pretty much nothing to do with environments. I am now using 3.9.1 and all is gucci.

Update: After using which python in my jupyter notebook and in my Terminal, I see that they are both using the same environment. As such I am still at a loss as to why my notebook cannot find json_lines.

I have two python environments on my computer, a default one and one I have for running my jupyter notebook on. I am trying to install the library, json_lines to the latter environment. I am not used the Anaconda environment manager.

On my Mac's Terminal I used the general pip install command pip install json-lines, but when I try to execute the following line of Python import json_lines in my notebook, I still receive the following error ImportError: No module named json_lines.

As I suspect I am not installing to the correct environment, I tried installing the library from inside my notebook with the following, import sys; !{sys.executable} -m pip install jsonlines. However, this has not changed my dilemma.

Is there some way I can specify from my Terminal which environment to install to? or is it likely I am encountering a different issue to what I suspect?

Con O'Leary
  • 99
  • 1
  • 10
  • try without the underline _ : `import jsonlines` – Timinator Jan 17 '21 at 16:45
  • (I feel that this question must be a duplicate but) Can't you simply activate the desired environment first?) – user202729 Jan 17 '21 at 16:45
  • [pip - Installing python packages in virtual environment - Stack Overflow](https://stackoverflow.com/questions/27732911/installing-python-packages-in-virtual-environment) ? – user202729 Jan 17 '21 at 16:47
  • Have tried without the underline. How does one activate an environment? Yep, I would not be surprised if the technical essence of my question is a duplicate, but then again I am sure others will search for this issue through the lens of the same false assumptions I have – Con O'Leary Jan 17 '21 at 16:51
  • Perhaps [python - How to install a package inside virtualenv? - Stack Overflow](https://stackoverflow.com/questions/21240653/how-to-install-a-package-inside-virtualenv) is a better duplicate (there's instruction on how to activate in the accepted answer, if you're using any POSIX system) – user202729 Jan 17 '21 at 16:52

2 Answers2

1

The package for json_lines in pip in json-lines. Hence you could install it as:

$ pip install json-lines

It may be appropriate to use an isolated python environment for your particular project if you want to use particular conda libraries but without the whole package. In this instance, you would be able to use virtualenv. This will allow you to create an isolated python environment.

$ pip3 install virtualenv

You can call virtualenv to create a virtual python environment with the working name e.g. myvenv.

$ virtualenv myvenv

From here, you can set your terminal to use this python version. If you are on *nix:

$ which python
/usr/bin/python
$ source myvenv/bin/activate
(myvenv)$ which python
/.../myvenv/bin/python
Eoin Dowling
  • 373
  • 3
  • 15
  • Thank you. Using 'which python' I can see that I am using an environment that my notebook isn't. How can I find the paths for my environments? so that I can change environment using the other command you provided – Con O'Leary Jan 17 '21 at 16:59
  • Within your Jupyter IPython repl, you can use the command ```!which python```, this should help you locate where your Jupyter python binaries are located. – Eoin Dowling Jan 17 '21 at 17:06
0

This article can help you out. https://janakiev.com/blog/jupyter-virtual-envs/

You need to create a virtualenv which will be used by your notebooks.

targhs
  • 1,477
  • 2
  • 16
  • 29