Fixing Python on Mac - python

Getting Python right on Mac seems a constant challenge. I'm working with a Homebrew implementation, and now have difficulty running Jupyter except with sudo:
$ which python
/usr/local/bin/python
$ which pip
/Users/username/bin/pip
$ which jupyter
/usr/local/bin/jupyter
$ jupyter notebook
Traceback (most recent call last):
File "/usr/local/bin/jupyter", line 7, in <module>
from jupyter_core.command import main
ImportError: No module named jupyter_core.command
$ sudo jupyter notebook
The Jupyter Notebook is running at: http://localhost:8888/
Once running, Jupyter fails to import pandas (installed via pip install pandas):
import pandas
ImportError Traceback (most recent call last)
<ipython-input-18-d6ac987968b6> in <module>()
----> 1 import pandas
ImportError: No module named pandas
.. even though pandas is available (python -s 'import pandas' works fine). Guessing its path isn't available to root.
I'm wondering if Jupyter is particularly problematic or if this is just my setup..?
Grateful for assistance as this is becoming very tiresome. Any guidance on wiping python and reinstalling 'properly' from scratch would be handy.
Edit:
$ which virtualenv
/usr/local/bin/virtualenv
21:16 $ virtualenv
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv import main
ImportError: No module named virtualenv

What python is in the shebang (#!) line on pip and jupyter? You may have installed jupyter with a pip that was using /usr/bin/python. Which means it's not there in the libraries installed for Homebrew's /usr/local/bin/python.
You should be able to resolve this with something like this:
/usr/local/bin/python $(type -p pip) install jupyter
But in general I second #Ray's suggestion of using pyenv to manage a personal set of Python installations that you can switch between, independently of what's on the system. (I likewise use rbenv for Ruby and perlbrew for Perl.)

Stop using python via native install on OSX or via homebrew right now!!!
First learn to use pyenv to control your version of python being used. Pyenv allows you to safely switch between any version of python you want at any time without messing up the natively installed python (which may be needed by the system): https://amaral.northwestern.edu/resources/guides/pyenv-tutorial
Second learn how to use virtual environments (pyvenv) to sandbox your projects. Virtual environments will allow you to have different sets of libraries installed by pip and such available as needed: https://robinwinslow.uk/2013/12/26/python-3-4-virtual-environment/
With these two technologies in your toolbox you will never have the issue you are experiencing again.
Please note pyvenv and pyenv are two seperate technologies used to enable similar goals (to control/sandbox environments) with unfortunately VERY similar names. One thing to note is you'll see older references to virtualenv if you google around a bit. This is the same thing as pyvenv.
Someone put together a good overview of using these with Jupyter: http://www.alfredo.motta.name/create-isolated-jupyter-ipython-kernels-with-pyenv-and-virtualenv/.
The other option is to do everything in virtual machines (with say virtualbox) or better still Docker containers, but this a whole other ball of fun best left to after you master virtual environments (you'll want them when you build your docker images anyhow).

Related

ModuleNotFoundError: No module named 'tensorflow' Vs code

I am running on windows 10. I am having problems with importing TensorFlow. I am in Vs code. I installed tensorflow using pip3. And here is the error:
Traceback (most recent call last):
File "c:/Users/USER/PycharmProjects/tt/main.py", line 1, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
I created my project in pycharm.
I am using python 3.7.5 and TensorFlow 2.0.0. This is how I installed TF:
pip3 install tensorflow==2.0.0
please help I was having this problem for a week.
Thanks , In Advance
For your work/project, please try to create a new virtual environment and then install all your required packages.
Like this:
$ python3 -m venv env
Activate it:
$ source env/bin/activate
and then install the tensorflow package inside your virtual environment:
pip3 install tensorflow
I think you need to create environment separate
and then install tensorflow..
for more information regarding envirnoment Link
Do you have several different python environments on your pc? Which python interpreter are you using when you install tensorflow?
The Status Bar will show you the current interpreter in the lower left corner. Check whether it is the same with the one you use when you install tensorflow.
enter image description here
I was getting your exact error, but this worked for me:
File->settings
Workspace tab
Search for "interpreter"
Put the complete path to the python.exe you want to execute under "default interpreter path"
It seems VSCode either found (or included) an older version of python that couldn't find the things I installed with pip.
I am a bit of a python newb and I'm guessing the venv approach is superior, but I couldn't get that cooperating with VSCode on windows.

I Get ImportError: No module named pathlib, even after installing pathlib with pip

This is my first time asking on this site, so sorry if my question is not layed out correctly
y#DESKTOP-MQJ3NCT:~/Real-Time-Voice-Cloning$ python demo_toolbox.py
Traceback (most recent call last):
File "demo_toolbox.py", line 1, in <module>
from pathlib import Path
ImportError: No module named pathlib
I have tried:
pip3 install pathlib
and:
sudo -H pip3 install pathlib
but continue to get the same error
I am using the windows store version of ubuntu 18 LTS and python 3.7
When it comes to python, it's quite easy to make the mistake of just running "python ...". When you install python on windows "python" defaults to the python 2.7 installation ( probably changed now that 2.7 is no longer supported) if it is installed.
Ubuntu has the links "python2" and "python3" which makes so much more sense but can still lead to confusion.
If you have a local python Virtual environment, the "python" command defaults to the global install on windows (to further confuse people).
I find it generally best to create my own links to the global python "python27" and "python36" to avoid these confusions.
Same goes for pip. It's best to call
python3 -m pip install ...
Also. PyCharm is the most amazing Python IDE in the world and it helps with so much.
Sorry, turns out it was a simple mistake!
Instead of:
python demo_toolbox.py
I should have used
python3 demo_toolbox.py
As Linux defaults to Python 2 or something incompatible with pathlib

Python not finding its Packages

I wrote myself a handy bash script, which solves the task of creating a virtualenv with its own compiled virtualenv and python. It aims at creating a mostly self contained virtualenv, with maybe only native libraries installed in system level if necessary, but installing all python packages and virtualenv and pip and such things inside the virtualenv.
The script can be found here.
I invoke the script as follows:
self_contained_venv.sh \
-n udacity_model_building_and_validation \
-p https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tar.xz \
-v https://pypi.python.org/packages/c8/82/7c1eb879dea5725fae239070b48187de74a8eb06b63d9087cd0a60436353/virtualenv-15.0.1.tar.gz#md5=28d76a0d9cbd5dc42046dd14e76a6ecc \
-d pandas scikit-learn seaborn
Given the required packages for compiling python and virtualenv are installed on the system, the script creates a nice virtualenv. However, when I try to access any installed modules/packages from within the virtualenv, python is not able to find them. To demonstrate this, I'll put some output of commands and code here:
First of all of course I have to activate the virtualenv:
. bin/activate
output: None, works without problem.
Then I print the pythonpath python is aware of:
import sys
for i in sys.path:
print(i)
output:
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python34.zip
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/lib-dynload
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So far so good. Then I try to import a module / package I installed during usage of my bash script: pandas:
python
(IDLE is running)
import pandas as pd
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Another try:
import numpy as np
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
Huh? So none of the packages is available? Lets check pip again:
which pip
output:
alias pip='localpython/bin/pip3.4'
./localpython/bin/pip3.4
Ok, so it's using my local pip.
Check packages:
pip list
output:
numpy (1.11.0)
pandas (0.18.1)
pip (8.1.2)
psutil (4.1.0)
Python-contrib-nbextensions (alpha)
python-dateutil (2.5.3)
pytz (2016.4)
PyYAML (3.11)
setuptools (18.2)
six (1.10.0)
virtualenv (15.0.1)
Hm the packages are there, so why can't python find them? Let's see where those packages are located, simply by trying to remove one:
pip uninstall pandas
output (shortened, because it fills many pages):
Uninstalling pandas-0.18.1:
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas-0.18.1-py3.4.egg-info
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__init__.py
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/__init__.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/_version.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/info.cpython-34.pyc
Aha, so the packages are in the path.
Another attempt on finding out if python looks in the right places:
>>> from distutils.sysconfig import get_python_lib
>>> print(get_python_lib())
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So this one indicates it does not look in the right place, if I understand correctly.
Note:
The script does the following things, which might not be obvious:
compile python with a prefix (local python)
compile virtualenv for the local python
it aliases pip to the local pip of the version of the installed python
it aliases virtualenv to the locally installed one
it installs packages from pypi if specified
it updates the local pip if there is a newer version available
I am a beginner still at writing bash scripts, so I think the structure and logic of the script is fairly easy to understand. It also prints information about success of its operations in the terminal.
Further Notes:
I did not use su or sudo to run the script.
My OS is a Fedora 22, although I think in this case any major distro would work the same way.
OS has been updated recently.
Question: So why can't Python find them / its own packages? (What do I need to change?)
can you try to compare path to pyhton and pip in your script and manually in your system:
which python and which pip?
After that check pip freeze | grep pandas it should return you package ...if no you need to add this package to site-packages folder of you python.

checking virtual environment modules

So I am following this tutorial and I am about to give up since i am working on a windows machine and I am pulling my hair out finding equivalencies.
I have a virtual environment, but when I try to run my code it dosent appear to be finding the local dependancies!!
(venv) PS C:\Users\Vince\skellys\Flask_skelly\venv> python Bartender_no_ui.py
Traceback (most recent call last): File "Bartender_no_ui.py", line 7, in <module>
import lxml
ImportError: No module named lxml
How can i check if the dependencies are there and how do i make sure my code is running from venv?? Am I just to much of a noob to be trying this tutorial anyway??
maybe relevant details- I had to use Conda rather than pip to install lxml, because pip couldnt get the dependencies of libxml2 and libxslt, but I get no module error for the other modules that I installed in the venv using pip as well
If you do
pip freeze
That should show you what modules you have installed in your virtualenv as long as you have it active.
Seeing as how you are working on windows I'd check out this site Its what I followed when I first got started.
Haven't used Conda much myself so not familiar with how it works but I'm guessing that won't install to your virtualenv. If you download lxml from here you should be able to install it into your virtualenv on windows.
its not the best solution but I figured out a hack around - I copied lxml from my c:\anaconda\pkgs folder. Code appears to be running fine. figures.

IPython pointing to wrong version of Python

I'm running Ubunutu 13.04. When using IPython, it points to a 3.3.1 copy of python that was installed in /usr/bin. I recently installed the most recent release of Python (3.3.2), which installed itself to /usr/local/bin. This means that typing 'python3' into the terminal, python3.3.2 comes up, but when I type 'ipython3', it uses 3.3.1.
I know this doesn't affect my programming (I'm not advanced of a user enough to make use of any of the differences between .1 and .2), but I'd still rather tidy my system up and use the most recent version. I tried using the fix given here, changing
#!/usr/bin/python3
to
#!/usr/local/bin/python3
But the following error is given:
Traceback (most recent call last):
File "/usr/bin/ipython3", line 5, in
from pkg_resources import load_entry_point
ImportError: No module named 'pkg_resources'
So the questions are:
1) How can I make python3.3.2 install over 3.3.1 (ie not in /local/)? I have no need for 3.3.1 once 3.3.2 is going.
2) How can I change IPython to point to python in /local/?
3) Which one of these options is better?
While this is certainly not a very neat solution, if you are really desperate, you can backup /usr/bin/python3 and use ln -s to create a link from /usr/bin/python3 to /usr/local/bin/python3, as such:
mv /usr/bin/python3 /usr/bin/python3-backup
ln -s /usr/local/bin/python3 /usr/bin/python3
(you might need to use sudo)
Be warned that this might mess up automated uninstalls et cetera, so rather use this as a temporary fix

Categories