Trying to make sense of Python/Jupyter environment on MacOS - python-3.x

Background: while running Jupyter Notebook a new import was failing even though the library was installing successfully using pip3. Some of the set up for the code I was running was done in PyCharm which was using a virtual Python 3.8.2 environment. The failing import library is in the virtual environment so why isn't Jupyter seeing it?
I went looking and found that there are multiple versions of Python installed:
/Library/Python/2.7
/Library/Frameworks/Python.framework/Versions/3.8
/usr/local/bin/python3
/usr/local/bin/python3.8
/usr/local/bin/jupyter (included this in case it clarifies things)
/usr/bin/python
/usr/bin/python3
/usr/local/Cellar/python/3.7.6_1
/Users/xxx/anaconda3/bin/python3.7 (anaconda was uninstalled months ago so why is this still here?)
/Users/xxx/git/moat-ds/venv/lib/python3.8
I have installed pyenv and virtualenv and tried (unsuccessfully) to sort things out through this and similar articles. But all of this has only left me with questions:
what are these different directories doing?
when launched what is Jupyter notebook using for 'python 3' kernel?
where are the python packages stored when I run pip3 at the CLI (in pycharm packages are put in the \venv folder but otherwise?)

installing jupyter with pip from pyenv fixed my problem
brew uninstall jupyter
pip install jupyter
and after restarting your console it should be pyenv's jupyter

After trying #Akbar30bill's answer without success I did brew doctor and restarted my terminal and tried again and it worked. Wasn't linked correctly or something.

Related

Unable to Install Jupyter due to legacy-install-error with psutil package

I was previously running into environment inconsistencies after downloading tensorflow with pip in a conda environment, and after several hours of frustration, decided deleting anaconda/miniconda and python and starting from scratch would be my best option to get python working again (The inconsistencies were causing a plethora of issues with loading other modules like numpy, etc).
After downloading Python version 3.9.10 directly from https://www.python.org/downloads/macos/, I wanted to see if I could start using Python again in a Jupyter notebook in VS Code. When I try to run a simple print('hello world') in a python chunk I (unsurprisingly) get the following error:
Jupyter cannot be started. Error attempting to locate jupyter: Running cells with 'Python 3.9.10 64-bit' requires notebook package.
Run the following command to install 'jupyter and notebook' into the Python environment.
Command: 'python -m pip install jupyter notebook -U'
I was pretty much expecting this error, since I knew I had deleted all packages when deleting Anaconda and Python.
However, when I try running python -m pip install jupyter notebook -U in the terminal I ultimately get the following error:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
error: command '/usr/bin/gcc' failed with exit code 1
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> psutil
note: This is an issue with the package mentioned above, not pip.
Any help or insight is greatly appreciated! I am on MacOS (M1) if that is useful information.
The problem seems to be with the python3 distribution that comes with MacOSX Command Line Tools. Well, at least that same error was gone after following these steps.
So, to fix the problem I installed python3.9 (I presume you can change 3.9 to your desired version, e.g. 3.8 or 3.10) using Homebrew.
Install Homebrew (to check if it's installed already just type brew --version in the terminal):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install python3.9
brew install python#3.9
Create your virtual environment
python3.9 -m venv myvenv
Activate the virtual environment with source myvenv/bin/activate
Finally, install Jupyter using
(myvenv) pip install jupyter notebook
I have the same problem when installing the Jupyter notebook on MacOS 12.5 M2 chip. It turns out that the error is caused by not having GCC on my mac to run the wheel for installing the psutil package because for some reason I removed my XCode from my Mac. So I installed XCode from AppStore and opened XCode after the installation to agree to its terms and conditions. After Xcode went through the setup process I tried pip install jupyter again and it worked well.
In my case, I just opened the XCode, accept the terms and the installation proposed, and tried to install the jupyter again and it worked!
It seems to be very particular about the version of python installed. I set up a fresh installation on a new box this morning and put 3.11 on there. pip install jupyterlabs gave me the same error as you got. I then uninstalled python and installed an earlier version, and now it works.
PS the older version that worked for me is 3.10.8

How to run anaconda and launch jupyter notebook with python 3.9.6?

I am on macOS Big Sur v11.5.1 with python3 v3.9.6 and since updating my OS and my Python can no longer run anaconda or launch jupyter notebook.
When I use the terminal command jupyter notebook I get:
zsh: command not found: jupyter
So I checked anaconda (which the terminal had said had been successfully installed) with anaconda --version and got:
zsh: command not found: anaconda
Prior to the Big Sur and Python 3.9.6 update (I was previously running 3.6) I was able to just type jupyter notebook and it would launch in my browser. I'd like to be able to do that again now. Is the issue that anaconda doesn't run on python 3.9? Or that the zsh shell isn't recognizing anaconda, conda, etc?
Thank you!
I guess your jupyter was installed in conda base env.
Find out where your anaconda installed.
For example, I installed it in home directory,
so I do following steps:
echo "source ~/anaconda3/etc/profile.d/conda.sh" > ~/.bashrc
then you have to
source ~/.bashrc
After that you will get back your conda.

Why virtualenv shows all packages installed and do not install modules in virtualenv?

Question
I am not sure why when virtualenv active, pip freeze stills showing all modules when it suppose NOT to. What am I doing wrong?
On the terminal
I tried two ways to create a virtual env:
virtualenv my-virtualenv
virtualenv --no-site-packages my-virtualenv
then activate it
source my-virtualenv/bin/activate
Results
Both show all packages (when they suppose not to). Running command pip freeze I get:
(my-virtualenv)$ pip freeze
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
actionlib==1.11.9
angles==1.9.11
bondpy==1.7.19
camera-calibration==1.12.20
camera-calibration-parsers==1.11.12
catkin==0.7.6
cv-bridge==1.12.4
diagnostic-analysis==1.9.2
diagnostic-common-diagnostics==1.9.2
diagnostic-updater==1.9.2
dynamic-reconfigure==1.5.48
gazebo-plugins==2.5.13
gazebo-ros==2.5.13
...
rosnode==1.12.7
rosparam==1.12.7
rospy==1.12.7
rosservice==1.12.7
rostest==1.12.7
rostopic==1.12.7
rosunit==1.13.5
roswtf==1.12.7
tf2-ros==0.5.16
topic-tools==1.12.7
xacro==1.11.2
Running
(my-virtualenv)$ which python
/home/user/.../my-virtualenv/bin/python
(my-virtualenv)$ python -V
Python 2.7.12
and
(my-virtualenv)$ which python3
/usr/bin/python3
(my-virtualenv)$ python3 -V
Python 3.5.2
Hence, I even can use python3 when virtualenv is activated but it uses the pc installed module. Moreover, new installed packages are install in the pc and not the virtualenv and I can't install python3 in the vitualenv because it "exists" already (but in the pc).
I was having the same issue and it was because, somehow, Python configuration of ROS was generating it.
I solved it removing the source commands of ROS from the ~/.bashrc. E.g.:
source /opt/ros/melodic/setup.bash
source $HOME/ROS/aslam_ws/devel/setup.bash
By the way, I realised that, before changing anything, creating the environment from PyCharm works properly too (when in the terminal I still had the issue).
There must be a better solution but I haven't found it yet.

import ortools in mac

I am trying to run some code using ortools on a python environment. I did not have troubles on a windows machine but I am having problems on mac (10.12.6). if in my virtual environment I run
pip freeze
or
conda list
ortools appears in my list of installed packages. But if I try to use it
ipython
from ortools.linear_solver import pywrapplp
I get an error saying that there's no module named ortools. If I go to
mac/anaconda3/envs/nameenv/lib/python3.6/site-packages I do have a folder called ortools with some python files including pywrapplp. Do you know what I am doing wrong ?
EDIT
following request from coments:
import os
os.getcwd()
returns '/Users/imac'
which ipython
/anaconda3/bin/ipython
Installing ortools is a bit of a headache. It was some days ago, I think I finally made it with
easy_install ortools
I think it is a problem with the path. I guess because I did not install it with conda it does not find the package. I got around writting:
sys.path.append('/anaconda3/envs/env_name/lib/python3.6/site-packages/')
at the begining of my ipynb. That way I can run ortools.
You could have several python interpreter installed (python2 and python3)
So if you want to use it with ipython, which seems to be bind on python3 in your case.
First check if you have pypi package ortools installed
ipython -m pip show ortools
If you got an error it means the package is not installed.
so you can easily install it using:
ipython -m pip install --user ortools
note: We provide Pypi ortools package (64 bits) for Manylinux, Windows and MacOS.
You can also rebuild it from source https://developers.google.com/optimization/introduction/installing/source

How to run Jupyter Notebook without Anaconda on Ubuntu?

There is a lot of information available to run Jupyter Notebook with Anaconda but could not find any info to run Jupyter without Anaconda.
Any pointer would be much appreciated!
Basically the process is as follows:
pip3 install --upgrade pip
pip3 install jupyter
jupyter notebook # run notebook
Run a specific notebook:
jupyter notebook notebook.ipynb
Using custom IP or port:
jupyter notebook --port 9999
No browser:
jupyter notebook --no-browser
Help:
jupyter notebook --help
Answer from the following sources:
SOURCE 1
SOURCE 2
See Gordon Ball's Jupyter PPA, the most actively maintained Jupyter PPA as of this writing with support for both Ubuntu 16.04 and 18.04 (LTS).
Installation: It's a Ball
Thanks to Gordon's intrepid efforts, installation of Jupyter under Ubuntu trivially reduces to:
sudo add-apt-repository ppa:chronitis/jupyter
sudo apt-get update
sudo apt-get install jupyter
Doing so installs the jupyter metapackage, providing:
A standard collection of Jupyter packages published by this PPA, including:
Jupyter's core and client libraries.
Jupyter's console interface.
Jupyter's web-based notebook.
Tools for working with and converting notebook (ipynb) files.
The Python3 computational kernel.
The /usr/bin/jupyter executable.
As W. Dodge's pip-based solution details, the browser-based Jupyter Notebook UI may then be launched from a terminal as follows – where '/home/my_username/my_notebooks' should be replaced with the absolute path of the top-level directory containing all of your Jupyter notebook files:
jupyter notebook --notebook-dir='/home/my_username/my_notebooks'
Why Not Acanaconda or pip?
For Debian-based Linux distributions, the optimal solution is a Debian-based personal package archive (PPA). All other answers propose Debian-agnostic solutions (e.g., Anaconda, pip), which circumvent the system-wide APT package manager and hence are non-ideal.
Installing Jupyter via this or another PPA guarantees automatic updates to both Jupyter and its constellation of dependencies. Installing Jupyter via either Anaconda or pip requires manual updates to both on an ongoing basis – a constant thorn in the side that you can probably do without.
In short, PPA >>>> Anaconda >> pip.
There are two ways to install Jupyter-Notebook in Ubuntu. One is using Anaconda, the other using pip. Please go through the below added link for details.
http://jupyter.readthedocs.io/en/latest/install.html

Resources