Can't import tf2 into jupyter notecook - jupyter-notebook

I have just installed tensorflow2, but when I open a jupyter notebook and import tensorflow, when I do print(tf.version) I get a 1.13 version.

If tf.__version__ gives you 1.13 is becase in the current environment you have TensorFlow 1.13 installed.
You can have a look at every package installed in the current environment by using pip freeze and looking at the list.
However, to be sure you are working in an environment where TensorFlow 2.0 is installed, you can create a virtualenv and install therein everything you need with pip.
Remember to install also the jupyter notebook package inside your virtual environment, otherwise you will execute the system jupyter notebook and it will load the TensorFlow package installed at system-level.
Hope it helps

Related

Jupyter Notebook not loading libraries

I have a conda virtual environment with Python 3.7.16 and several installed libraries such as 'lifelines', etc. In the conda console all the installed libraries are shown; however, when I open a Jupyter Notebook on the same environment and try to load, for example, the library 'lifelines', it gives me the error message ModuleNotFoundError: No module named 'lifelines'.
I have searched on Github and Stack Overflow, and tried several solutions such as this one and others, but I still can't import libraries on Jupyter Notebooks that are already installed in the conda environment.
Does anybody know how to solve this issue?
In fact, I needed to install Jupyter-lab and notebook in the same environment; otherwise it was searching for all libraries in the base one.

Manually install packages into ipykernel NOT with anaconda

So I want to avoid using anaconda. How can I download packages into an ipykernel I made? I have the location, I just don't know how to activate ipykernels. I see the option for making a new .ipynb file once I'm within the jupyter API but this doesn't help me add the libraries I want to keep isolated on my machine.
you can install packages inside jupyter note book by
!pip install pandas("Your package name")
in a cell and run it

Do I need to install Jupyter notebook in every virtual environment?

I isolate my data science projects into virtual environments using pipenv. However, running a Jupyter notebok does not access the local environment and uses the default IPyKernel. I've seen that you can register virtual environments from within the environment, but this requires installing the ipykernel package which itself requires Jupyter!
Is there anyway to avoid this and just use a single Jupyter install for all virtual environments?
Generally, you'd install jupyter once and do the following in your virtual environments:
pip install ipykernel
python -m ipykernel install --user
This isn't enough when you're running multiple Python versions.
There's a guide here that tries to address this:
https://medium.com/#henriquebastos/the-definitive-guide-to-setup-my-python-workspace-628d68552e14
It's not 100% failsafe, but it can help you avoid reinstalling jupyter notebook all the time.
I found that there are few problems when reinstall jupyter for each environment separately: i.e. pip install jupyter jupyterlab in new environments.
I had multiple issues (with and without Conda), where Jupyter would install packages to a different python environment when you use !pip install a_package_name within a cell. The shell environment still kept track of the non-environment python, and you can tell this by comparing the outputs of !which python and
import sys
sys.executable
Therefore, when you tried to import the package, it would not be available, because the cells used the environment python/ kernel (as it detected the venv directory).
I found a workaround that I'd appreciate feedback on. I changed pipenv to install virtual environments into the working directory by add to .bashrc/.bash_profile:
export PIPENV_VENV_IN_PROJECT=1
Now when opening a Jupyter notebook, I simply tack on the virtual environment's packages to the Python path:
import sys
sys.path.append('./.venv/lib/python3.7/site-packages/')
Is this a terrible idea?

can't import 'torchtext' module in jupyter notebook while using pytorch

I installed pytorch using anaconda3 and my created virtual conda environment named 'torchTest'.
I installed all the modules needed but, codes doesn't work in jupyter python.
I installed torchtext using
1.pip install https://github.com/pytorch/text/archive/master.zip
2.and also pip install torchtext too.
all I mentioned successfully downloaded in my MAC OS X, but can't get what's wrong with my Jupyter notebook..
After having the same issue with torchtext from within my jupyterlab, I opened an issue on Github at the jupyterlab project as well as at the torchtext repository.
My current solution is to add the PYTHONPATH from the Anaconda env.
The Anaconda path is usually like that $HOME/anaconda/bin
You can add it from within Jupyter Lab/Notebook like that:
import sys
sys.path.append("/some/path/to/add")
import torchtext

How to install Tensorflow for R

I had Tensorflow installed with Anaconda. Now I want use it in R and I need to reinstall Tensorflow, because the note here
NOTE: You should NOT install TensorFlow with Anaconda as there are
issues with the way Anaconda builds the python shared library that
prevent dynamic linking from R.
I already tried to uninstall from Anaconda and install with pip but its came to the same place in anaconda directory. Tesorflow is working from terminal but in R shows Error: Command failed (1)
Anybody can help me to how I can solve the problem? Should I uninstall anaconda and install Tensorflow using pip?
You have several options on what to do. Probably the cleanest one is to install a system-wide python (if not installed yet) and then create a virtual environment. This basically takes your system python binaries and moves them to its own compartment where everythign is isolated from the rest, incl. anaconda. Once you are inside an activated virtual environment you can install all the necessary Python appendages for TensorFlow. Once that is done, make sure you set up a correct environmental PATH for TensorFlow from where R can reach it:
Sys.setenv(TENSORFLOW_PYTHON="/path/to/virtualenv/python/binary")
devtools::install_github("rstudio/tensorflow")
Example of the path to where you installed the virtual environment project would be, I think, something like ~/minion/medvedi/venv_medvedi/bin/python.
This is no longer an issue, the documentation was updated too.
See here:
https://github.com/rstudio/tensorflow/commit/4e1e11d6ba2fe7efe1a03356f96172dbf8db365e
With the help of Keras, we can install the TensorFlow package in R.
install_keras()
library(keras)
devtools::install_github("rstudio/keras")
install_tensorflow(package_url = "https://pypi.python.org/packages/b8/d6/af3d52dd52150ec4a6ceb7788bfeb2f62ecb6aa2d1172211c4db39b349a2/tensorflow-1.3.0rc0-cp27-cp27mu-manylinux1_x86_64.whl#md5=1cf77a2360ae2e38dd3578618eacc03b")
library(tensorflow)
Keras is a high-level neural network API for deep learning from TensorFlow Google.
my suggestion is to install anaconda and create an environment called "r-reticulate".
you can do it using anaconda navigator or
reticulate::conda_create(envname = "r-reticulate")
then to check that env detected by reticulate, use reticulate::conda_python().it must return directory of python.exe for your env.
after that you can install tensorflow by install_tensorflow(). [not working in my case]
so I install the tesnorflow from CMD.
follow these steps:
open the cmd :]
activate the r-reticulate env using conda activate r-reticulate (you may need your directory to conda directory if you did not add conda to your PATH)
use : conda install -c anaconda tensorflow
now in R, you can use TensorFlow.
for installing Keras, you can use pip install Keras. [i tried install_keras() function after the installation of tensorflow, but it ruined my TensorFlow installation also]
Eventually I found the best and fast method to do it in R:
devtools::install_github("rstudio/keras")
library(keras)
install_keras(method = "conda")
install_keras(tensorflow = "gpu")
tensorflow::install_tensorflow()

Resources