unable to use preinstalled python packages in Jupyter notebook - python

guys,
here's the problem. I have got access to a server with jupyter notebook. There are already several packages installed on this server, which I want to use. But unfortunately it doesn't work with all packages. E.g. pandas, numpy and others work but I have problems with sklearn and Tensorflow.
When I query the list of all installed packages via pip I see for example the installation of Tensorflow:
If I want to import the package I receive this message:
It's the same with sklearn. Other packages work. Since there are still several people working on this server I wouldn't like to reinstall packages or restart the server. Does anybody have an idea where my mistake is?
Thanks a lot and stay healthy!

There could be multiple versions of Python installed on the server.
And the version that pip is using does not have to be the same version as your notebook is running.
So first, check which version of Python your notebook is using; Open the File menu and click on New console for notebook. In the console you should see the Python version. Say it shows this:
Python 3.7.6
Then call python3.7 -m pip list to see which modules are actually installed for that version of Python.
Edit
Is there an easy way to tell only my juptyer notebook to take this version?
Use Change Kernel... from the Kernel menu.

Related

can't open dataframe in variable explorer in Spyder IDE

Due to Anaconda didn't support free for commercial use. So I downloaded Spyder IDE from Spyder official website.
Spyder official web
Since Spyder IDE doesn't provide a function for users to download libraries as PIP install. But I still love Spyder's UI and some functions, especially, variable explore, etc...
So I installed official python on my PC, taking Spyder IDE as a code editor. <-- It can work by changing the python interpreter in the Spyder preference setting.
But I found an error when I try to open a variable (which is dataframe) on variable explorer, others are work well.
from pandas import DataFrame
People_List = ['Jon','Mark','Maria','Jill','Jack']
df = DataFrame (People_List,columns=['First_Name'])
print (df)
I got the same error and I noticed that I only got it after I created GeoDataFrames from the Geopandas library. Once I restarted Spyder, as long as I don't create a GeoDataFrame, it opens all other DataFrames (from Pandas) just fine.
I had the same issue and setup (windows 10, trying to install and get geopandas to work, but then couldn't get dataframes to be viewed in spyder). Ultimately, running pip install spyder in the new python that I was trying to use in spyder fixed the problem.
However, I think probably switching to an anaconda-based python interpreter in spyder, and installing geopandas and spyder-kernels through conda, would be a better long-term approach. This also has worked for me (I think I have become confused between pip and conda, e.g. this answer re: geopandas asks for pip https://stackoverflow.com/a/58943939/5233269, but conda can also install geopandas -- and most importantly deal with all the dependencies involved. I think conda is probably better long-run for data work.)

How can I install CPLEX for Python so I can access it via Jupyter?

I have already downloaded the CPLEX Studio. I am an absolute command prompt beginner, so following IBM's tutorials to install the API is proving to be very difficult. I want to get to the point where I can import cplex in Jupyter. I have reviewed other similar questions, but those answers are difficult to understand (for me). Is anyone able to explicitly explain how to do this in layman's terms?
If you can provide your OS, CPLEX version, python version and how you run jupyter,
I can provide you the exact commands to run.
You basically want to have a command prompt setup with the same python interpreter
you use for jupyter. Assuming CPLEX 12.10, you want to
$ cd $CPLEX_STUDIO_DIR1210/python/VERSION/PLATFORM
$ python setup.py install
(where VERSION is your python version, likely 3.6 or 3.7, PLATFORM is your OS platform)
For reference the doc is here: https://www.ibm.com/support/knowledgecenter/SSSA5P_12.10.0/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/set_up/Python_setup.html
You can do the installation from a jupyter notebook.
Just create a new notebook, then:
If you are using the Community Edition, just create a cell with:
!pip install cplex
If you want to use an edition already downloaded on your disk, create a cell with:
%cd "C:\Program Files\IBM\ILOG\CPLEX_Studio1210\cplex\python\3.7\x64_win64"
!pip install .
You need to do this only once.
After pip finishes, you just need to restart your jupyter.

Trying to install packages with pip properly

This is a very vague question but I am really stuck. I have been working with python for a little bit to try and use some of their functions in opencv (cv2) and other open source libraries. But whenever I try and use pip I will always get an error about improper installation or more commonly when trying to import the installed package idle throws an error that a line of code in the library that is causing an exception. This does not just happen to one library but nearly all of them (I have tried stuff like opencv, tensorflow, urllib). I have tried reinstalling different versions of python (multiple times) and reinstalling it but none of that seems to work. I don't know what to do but really want to be able to use these tools. I use windows 10 and use the default "User" user on admin.
Any help on possible options would be most appreciated!
This was happening to me as well.
In my case I wanted to work with Anaconda and Spyder. I was following some books and tutorials saying how to install stuff with the pip and so on, but it didn't work. What solved it was to install PyCharm and use those same pip functions directly on the PyCharm console (not the windows cmd). Then everything worked automatically and I have whatever library I installed from PyCharm available at Spyder without doing anything else.
Summary:
Get Pycharm -> link
use the same pip install directly in the PyCharm console
A-Hopefully it will work
If it doesn't: Can you elaborate on which IDE are you using? Most likely there is someone here who can help you.

Package not found in python, despite loading jupyter notebook from environment w/package

I have a fresh installation of Anaconda on OS X, and I'm following the instructions on this page to the letter, in order to create the appropriate environment.
When I enter conda list after activating the environment, pandas and matplotlib are both listed.
But when I open the provided jupyter notebook, or when I create a new jupyter notebook and type import pandas as pd, I get a 'there is no module called pandas' error. Ditto for matplotlib.
Happy to provide any more information that would be helpful in figuring out the problem. I feel like I run into problems like this a lot, so general strategies for debugging this kind of thing are welcome. Thanks!
EDIT: OK, I've fixed the problem for now, but I still feel like I'm missing something.
The problem was that there was no jupyter/ipython notebook kernel for the version of python in this env. So when I launch the notebook, even within the env, the correct version of python doesn't load, and some packages aren't available.
The solution is to add the kernel as described here: Using both Python 2.x and Python 3.x in IPython Notebook
It seems a bit odd to have to install a different ipython-kernel for every environment, but maybe it's not. I guess I just thought the correct version of python would load in the notebook as long as the notebook was launched from the env.
Does anyone have a better solution?
Since both your Py2 and Py3 kernels need different, version specific sets of packages to support them, the best and most convenient solution seems to be the one it sounds like you followed -- use anaconda, install either version as your default, then install an env with the other. If you want pandas available to both versions, you will need to make sure you conda install it in both the root and the other env. Jupyter is a bit magical in that it can be launched from either environment, yet will recognize that an ipython kernel exists in a different env and make both available to you.
It sounds like this may have worked against you slightly, though, and that you either launched jupyter from an env that didn't have ipython installed, but did have pandas. Jupyter found the ipython kernel in the other env, but you didn't have pandas installed there yet. At lest thats a theory. Solution remains to install pandas and ipython in both. Doing conda install anaconda in both envs will give you lots of packages, if space isn't a concern and may save you some of this aggrivation.

How do I change the kernel/python version for iPython?

I have installed iPython using pip in OS X 10.10, and it gave me the "ipython" and "ipython2" commands, which run great, but which use OS X's default python version 2.7.9. I downloaded and installed the latest release of Python3.4 and can load it with the command "python3," but cannot find a way to get iPython to use this version of python. The iPython Web site states the package can be used with python versions 3.3 and above, but I cannot find any instruction on how to change the default python version used.
So far I have found that the jupyter package for iPython has a kernel specification in /usr/local/share/jupyter/kernels/, which is just a folder called "python2" containing a json file that points to the system's python 2.7.6, but altering this to point to the new python3.4 installation does not work. My guess is this configuration is for the ipython notebook.
I've also tried the approach here: ipython reads wrong python version
In doing so I've duplicated the ipython2 command in /user/local/bin/ and edited it to use the python3 interpreter located at /Library/Frameworks/Python.framework/Versions/3.4/bin/python3, however, this gives me an error "ImportError: No module named 'IPython'," which suggests the python3 installation does not have ipython installed.
To tackle this, I've tried uninstalling ipython and reinstalling it using pip, but it just targets the system's Python 2.7 installation and does nothing for python3.
Does anyone know how to configure iPython to use a different python version, or even install a separate ipython installation for python3? Ultimately it would be nice to quickly switch back and forth depending on my needs.
I just found the answer. In essence, this stems from not understanding the python installation layout and how resources are separated between installed interpreters. It appears each python version will have its own repository of tools, and the current "pip" command I had installed on the system was mapped for use with python 2.7, so all libraries, tools, and other details it managed where available only to python 2.7. This included iPython.
I thought that installing python3 would make all these libraries available to the new interpreter by default, but it appears the system keeps them all separate (which makes sense). The main issue here was continuing to use "pip" which targeted the old installation, instead of the new "pip3" command included with python3. By using pip3 to reinstall iPython, I was able to get iPython3 installed properly.
Unfortunately this setup means needing to re-download other libraries, but that's not too difficult.
It might seem like a trivial issue in hindsight, but this had me completely stuck. I hope this helps someone else in this situation.

Categories