Importing library error: No module named <pkg> - python

In the past, I downloaded an open-source package through pip when I was just using it and it worked fine but I have recently started to help with the development of the package so I cloned the github repository onto my laptop and am now having troubles. I checked out and pulled the changes to a new branch in the github repository to work with some changes someone else made but my Jupyter Notebook was still using the old version of the package as I couldn't access the new methods in the notebook I was working in. If I open the code through 'open .' when in the directory, I can see that it is the proper code that I need to run but Jupyter notebook was not running that code. I then ran 'pip freeze' in my terminal and saw a package was still installed there so I ran 'sudo pip uninstall ' and uninstalled the package from pip. Although now, when I try to import the package into my Jupyter Notebook I get the error where there is:
No module named <package name>
Does anyone know why this is happening and how I can fix it? I would like to be able to use the version of the code of which branch I am in currently.
EDIT:
The package library is stored in /Users/myName/pkg. I am trying to access this through the 'import ' statement in Python. I am running this import statement through a Jupyter notebook in Jupyter lab. Jupyter lab is installed under python3.7/anaconda3/bin/jupyter-lab. I used to have another version of this open-source library installed which I installed using pip, but I recently uninstalled it as I thought that was causing the problem.
I also ran
sys.executable
and it returned '/anaconda3/bin/python'
EDIT 2:
I tried following the solution from this post but when I activated my new environment created, my entire jupyter lab local-host page just kept refreshing constantly and would not load anything. Although when I do not activate that environment I can run my old jupyter lab fine.
EDIT 3:
SOLUTION:
The solution I linked above worked for me, although I also had to update my Jupyter Lab in the new conda environment to stop my page from being infinitely refreshed.

Related

How can I get a Jupyter Notebook to see the modules in the environment that it was opened in?

I am attempting to import cartopy into a notebook but am running into issues. If I do a "conda install cartopy" in my base environment, I get the frozen/flexible solve issue (https://github.com/conda/conda/issues/9367). When I open a new environment and do a "conda install cartopy", it seems like everything works. "conda list" shows version 0.18.0 in that environment. Then I open a Jupyter Notebook from within that environment and try to import it into the notebook but originally got the response "no module named cartopy". I tried fiddling with my environments and settings based on feedback on this page (In which conda environment is Jupyter executing?), but now the error is "no module named numpy"! Can someone please help me understand why the notebook isn't seeing these modules? Thank you.
Are you certain that numpy is installed in this new environment?
Given that you are using the Anaconda distribution of Python, you should be able to view/configure your environment and it's installed modules using the Anaconda Navigator. Here you can see a full list of all installed modules, and via the terminal/console, launch a Jupyter Notebook from not only within a specific environment, but within a specific directory!
On the left hand side, you can choose the environment that you want to use to start Jupyter Notebook, and on the right you can view all of the installed modules in that environment. Make sure that jupyter, numpy, and cartopy are all listed as installed packages.
To make sure that I am launching Jupyter from my desired environment, I always launch it directly from the terminal. I "Open Terminal" with my environment, "cd" into the directory that I want to use, and then use the jupyter notebook or jupyter lab command.

"r.start is not a function" Fermipy Conda Error

I've been using the Fermipy conda environment on Python 2.7.14 64-bit on macOS Catalina 10.15.5 and overnight received the error "r.start is not a function" when trying to connect to the Jyputer server through Vscode (if I try on Jupyter Notebook/Lab the server instantly dies). I had a bunch of clutter on my system so I ended up formatting it and reinstalling all the dependencies needed (such as Conda through Homebrew, Fermitools through Conda and Fermipy through the install script on their site), but still get the same error, although I was previously running python scripts just fine. It gives me no other error or output, if it did I would attach it here. This is the error I get.
Edit: I get the same error using any version of Python 2.7.XX and not for python 3.7.XX.
As answered here, https://github.com/microsoft/vscode-python/issues/12355#issuecomment-652515770
VSCode changed how it launches jupyter kernels, and the new method is incompatible with python 2.7.
Add this line to your VSCode settings.json file and restart.
"python.experiments.optOutFrom": ["LocalZMQKernel - experiment"]
I got the same message. (r.start is not a function.) I had an old uninstalled version of anaconda on the computer which had left behind a folder containing its python version. Jupyter was supposed to be running from new venv after setting both python and jupyter path in vscode. I fully deleted remaining files from old anaconda install - message went away and notebook ran fine. Maybe try getting rid of all conda stuff and pip install jupyter and anything else you need.

Importing updated package in python

I've updated scipy package in my environment from the terminal and started the Jupiter notebook.
In a cell I run
scipy.__version__
but that shows the previous version which doesn't have some properties I need. What can be the cause of the problem?
screenshot
There might be several versions of the package still installed on your machine.
Restart completely the Jupiter notebook (even your computer). Make sure the jupyter notebook is runned in the correct environment where you have updated the package if you are using for example anaconda.
Search for the package name a list all the folders in which is it present.

Pycharm jupyter notebook wsl: Jupyter package is not installed

I would like to use Jupyter notebook inside Pycharm. The project interpreter is a python2.7 from a virtual environment inside WSL (ubuntu 18.04).
The Jupiter package is correctly installed inside the virtual environment (I can run it by jupyter notebook).
My problem is that when I want to use Jupyter notebook inside Pycharm, I get the following error: Run Error Jupyter package is not installed (see picture).
Any idea what's going on here?
I had this problem in Python 3. Below are the steps I took to resolve the issue; I believe they should resolve the issue for you too:
I had Jupyter Lab installed. Pycharm only works with Jupyter Notebook. Long story short, if you have Jupyter Lab installed you need to uninstall all your packages using:
$ pip freeze | xargs pip uninstall -y
Restart your computer
Follow Jupyter Notebook installation instructions
Make sure WSL is set up through pycharm instructions: wsl pycharm instructions
In Pycharm, open an .ipynb file. Click the dropdown that says "Managed Jupyter server" It's right above the text editor. Select "configure Jupyter server". Check configured server.
In your wsl terminal, type jupyter notebook. Copy and paste the text that looks like: http://localhost:8888/?token=874asdf687asd6fasd8f74ds6f4s9d8f7sddf into the cofigured server box in Pycharm.
That's it. You should be able to run the jupyter cells in pycharm now.
I have Pycharm 2020.3 For me the issue was I was using a virtual environment with "inherit global site packages." I had Jupyter installed in global site packages but NOT the virtual environment.
Once I installed Jupyter within the virtual environment Jupyter notebook worked. Not sure why inheriting Jupyter from global packages wasn't working for me.
The above solution using a designated url with token seems to work with older versions of PyCharm. A simpler solution is to upgrade to the latest PyCharm. I no longer had an issue with the auto server using PyCharm 2019.3.2 (Mac)
I had this problem with Datalore plugin enabled on 2020.2 linux, running on bare metal but displaying to a remote X server (probably doesn't matter). My solution was to disable the Datalore plugin (it's enabled for professional pycharm by default).
This way I was still able to use the "managed" auto-start version with better integration / debugging vs the "configured" option (or at least with less hassle).
Note since it's been a year, my problem is probably different than OP.
This happened for me, when the interpreter was a remote one. I fixed this by changing the interpreter to one from a local env.
This can be done by selecting the Configure Jupyter Server.
I also meet this problem,and i solved it
i create the new project with the global sit-packages like below
then i meet the problem
i create the new project with no global sit-packages and install jupyter notebook in the virtualenv
then the problem is gone

Tensorflow: Trouble importing Tensorflow in a Jupyter Notebook

I have a conda environment with Tensorflow-gpu installed for python 3.6.6. I am running Ubuntu linux 16.04 LTS. When I activate the environment and start a python shell, I can import Tensorflow just fine. But when I start up a Jupyter notebook or Jupyter lab session, I can't import Tensorflow.
Now there is a pre-existing SE post on this issue. However, that post is 2 years old, so there are changes in Jupyter since then. Second, I tried the fixed mentioned in that post, and none of them have worked. So I was hoping there might be some progress or some new ways to fix this.
So let me provide a little more detail on the issue.
First, when I activated a python 3.6.6 environment in anaconda and did conda list in a regular Tilix terminal session, I can see that Tensorflow-gpu is listed.
When I start ipython in a standard Tilix terminal and type import tensorflow as tf, that works fine.
Next, I started a jupyter lab session and opened a terminal from the jupyter launcher. In the terminal I started an ipython session and tried to import tensorflow. That does not work.
I opened a new jupyter notebook and tried to import Tensorflow then I "ModuleNotFoundError: No module named 'tensorflow'" error.
If I just run a python file with tensorflow code from the regular tilix terminal, then it will run fine and find Tensorflow. So only the notebook seems to be the big problem.
Things that I have tried:
I did try to implement a few of the fixes suggested in the older SE post, as well as the closed github issue on this problem.
I tried to reinstall tensorflow
I did a conda install of ipython, jupyter because some folks suggested installing those into the environment directly.
I looked at the jupyter kernelspec list. Some folks suggested making a change to this, but it was not clear what to change.
I installed the conda install -c anaconda-nb-extensions nb_conda in the environment too, but still no luck.
Anyone else have a suggestion about how to fix this.

Categories