Conflicting Python Installs in OSX Mavericks - python

I've got myself in a pickle and would like some guidance before my laptop suffers GBH.
I have been using my Macbook for a few years without probs but when I got a new iMac at work I noticed everyone recommended useing homebrew for new Python installs (esp. on Mavericks).
Now my laptop has worked fine with the original Python. But I decided to try and do it the new homebrew way (its now Mavericks btw).
First I clear out Mavericks due to some other conflict then reinstall a fresh. Get nginx, php-fpm, mysql, etc working.
Now Python.
I can use pip to install packages. But when I try
workon myproject
I get:
/usr/bin/python: No module named virtualenvwrapper
/usr/bin/python: No module named virtualenvwrapper
But then it switches to that virtualenv anyway - but not the directory that the project is in!
When I run where python I get several entries:
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
I've tried fiddling with my path but keep getting other errors that all seem to indicate my two Python installations are conflicting badly. Is there a simple solution or do I need to wipe out hombrew and start again from the beginning? Or just give up on homebrew and use OSX built in?
To clarify my current $PATH is:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/op‌​t/X11/bin:/usr/local/go/bin:/Users/me/Development/Android/sdk/tools:/Users/me/Dev‌​elopment/Android/sdk/platform-tools

My solution was an unusual one but here goes:
In my .zshrc file I had the following layout:
ZSH=$HOME/.oh-my-zsh
ZSH_THEME="steeef"
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
The problem was the Oh My ZSH plugin virtualenvwrapper was looking in the default python path location as the custom path had yet to be set later in the .zshrc file.
The solution therefore was to move the PATH declaration before the plugins like so:
ZSH_THEME="steeef"
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
Now the ZSH plugins are referencing the correct Python install and therefore the correct Python packages path.

Try putting your preferred version earlier in your PATH variable. So if you want to use /usr/local/bin/python, from the command line you could modify your PATH by export PATH=/usr/local/bin/python:$PATH. See here for more info: http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/

Related

Why is Python Installation in "Library" directory macOS?

I'm on a new Mac and installing Python. I believe I reinstalled my Python using Homebrew. The path, however, is unusual to me, and I'm wondering why it is located here:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
I'm used to Python being somewhere like this, instead (which is also is):
/Users/user.name/Library/Python/3.9/bin
What is different about the first installation? Are there any special considerations if my Python is located here?
/Library/Frameworks/Python.framework/Versions/3.x/bin/python3 is the Python distribution shipped with macOS.
It's usually a good practice to create your own Python environment for your projects instead of using the default Python installation (consider reading about pyenv).
If you want to use your Homebrew installation, run brew info python to find your Python installation path, then append the parent directory's path to your PATH variable by editing ~/.zshrc (assuming you're using zsh, the default shell).
$ brew info python
....
Python has been installed as
/opt/homebrew/bin/python3
...
# inside ~/.zshrc
...
export PATH="/opt/homebrew/bin:$PATH"
....

Can't seem to fix this issue of multiple Python Instances conflicting via PIP. How can I side step around this issue?

My MacBook Pro High Sierra has Python 2.7 installed as well as Python 3.7 both of which were downloaded from their binaries off the general Python website.
I'm trying to install an R console rtichoke; so like every time, I use:
pip install -U rtichoke
The response I get is consistent requirements already met. So it's installed. But in terminal upon typing the command rtichoke to get it to run I get:
bash: rtichoke: command not found
Essentially my installs via PIP/Python are unable to be used from the general terminal. This seems like an issue with my .bash_profile and PATH. But I'm not well versed with that to even attempt at a fix. Here is a copy of my current .bash including the automatically set PATHs from the Python binaries.
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
#Brew
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
#Alias
alias r="rtichoke"
I've gotten a few suggestions to do this in a virtual environment but I can't say I know too much about that either. Ideally I'd like to go fixing any inconsistent installs but nevertheless ANY HELP IS SO APPRECIATED. I've spent hours with this and CANNOT figure it out.

How can I manage mutiple python in Ubuntu16.04?

  In my Ubuntu16.04, there are python 2 and python 3 default. In addition, i have installed anaconda too. I am sucked by the 'python' cmd. Every time i use pip or pip3 install, I don't know where the package install, python2 or python 3? And I use conda install to install anaconda package. I also use anaconda env to manage different virtual env. But I think it mix with my local Python 2 and 3.
  For example, in directory /usr/bin, I found many soft links like this:
   When i try 'python' cmd, it just confuse me!
   Why python3m are local, shouldn't it be anaconda? Why python3 are anaconda, shouldn't it be local? Then I found that if I use ./python2 or ./python3, I found it is correct now!
  So I know it is caused by environment variables. I echo $PATH, Found it like this: /home/kinny/.pyenv/shims:/home/kinny/.pyenv/bin:/home/kinny/anaconda3/bin:/home/kinny/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/ant/bin:/snap/bin:/opt/maven/bin:/usr/lib/jvm/java-8-oracle/bin
   I have used update-alternative --config python to configure default python, but it doesn't work! It sames mixed with each others.
   Now I just want to install tensorflow 0.11 in local python3, because in anaconda it is 0.10 version by default. So how can I change this. I just want to use python python3 and python3m represents python2.7 python3.5 and anaconda python respectively, How can I do that! use pip and pip3 for local python2 and python3 respectively!
I ran into a similiar problem when setting up PyCharm Edu to work with Anaconda. I found that I had several versions of Python installed and it was very hard to keep track of which version the IDE was referencing. My CS professor gave me the advice of simply removing the versions of Python I didn't frequent. I now just have Anaconda installed; and use the Anaconda Prompt as my Python console. I also rely on PyCharm's IPython for the developer console. However, if you still want differing versions of Python installed (say your doing QA testing for older devices); there is the really helpful command: which python. When entered into the python console or Anaconda Prompt: which python will display the directory associated with the currently executing Python Shell. This enables you to better keep track of to what particular python.exe the current window is referring to.
Follow up to the comments mentioning using virtualenv and virtualenvwrapper.
Here are the official docs and a good blog post to follow for getting started using virtualenv's is here:
https://virtualenv.pypa.io/en/stable/installation/
http://virtualenvwrapper.readthedocs.io/en/latest/install.html
http://exponential.io/blog/2015/02/10/install-virtualenv-and-virtualenvwrapper-on-ubuntu/
Also, once you are setup you can create virtualenv's specifying which python installation you want to use.
which python3
returns
/usr/bin/python3
Then create a virtualenv with that python path. Where example_env is the name of the virtualenv.
mkvirtualenv -p /usr/bin/python3 example_env
Then activate the virtualenv using virtualenvwrapper.
workon example_env
Finally, install tensorflow and other dependencies with pip.
pip install tensorflow
the which command is very useful for finding the path to the executable that is first in your path. Zsh also has the where command, which will show you all instances of the given executable that show up in your path. For managing different python versions, you have a lot of options. The easiest for most people tends to be anaconda, using conda environments. The installer will ask you to add some stuff to your .bashrc file, which will then make anaconda's binaries come first in your path. Anything else you run after the .bashrc gets sourced after that, will then use that first, including PyCharm. For graphical desktop apps to pick up the change, you may need to log out and back in again. If you only need one version each of python 2 and python 3, you can just use the ones available via apt. Depending on your Ubuntu version, Python 2 is definitely installed by default as it is used by many system utilities, including apt itself. Some newer versions may also install python 3 by default, but I do not remember for sure. Another option is to install the versions of python you need in an alternate location, such as /opt/python/<version> and then using environment-modules (installed via apt install environment-modules) or Lmod to control which versions are being used, but that may or may not be easy/convenient to use with a desktop application such as PyCharm.
for TensorFlow, 1.11 is available in anaconda, but I don't remember if it's in the default channel or not.

Confused about Python installations, module installation, and interpreter

So yesterday I had to create a virtualenv in order to be able to install Python modules that wouldn't install thanks to OS X El Capitan's new SIP. I thought I did everything right, but today I'm reaching a different conclusion. I hope I can be clear about it.
my python custom install is at myname/learnp/imdb_module, this is where I created it with virtualenv. Edit: I later moved it to myname/learnp/ayr2/imdb_module.
However, when I try to run the interpreter, it seems to always default to the Python that is in Library or something along these lines. I found out about this because a certain module that I managed to install in this custom python env wouldn't import, when I checked what modules I have, it wasn't the same as what I expected.
Furthermore, it seems that ALL other modules that I wanted to install on the CUSTOM virtualenv were installed on the main python env, and that I wasn't installing those modules on the custom env all along.
Excuse me, but I'm very confused right now.
I know how to create a virtual env
I know how to activate it (it appears to the right on Terminal line)
I don't know how to install modules to my virtual env
I don't know how to make the interpreter run from the virtual env so I can do python operations that are only possible by using custom env modules
Any advice is much appreciated!
Update:
Followed Will Hogan's answer for troubleshooting,and I think something weird is happening, quoting my comment to his answer:
HI, thanks for taking the time to answer. This is basically the way I understood this. However, let me attach a screenshot: http://i.imgur.com/DfpngJq.jpg . Am I right to assume something is wrong here? My prompt is changed with the virtualenv named "imdb_module", but when I type in which python it doesn't list ayr2/imdb_module/bin but rather a folder with the path usr/bin/python, which if I understand correctly is the "default" environment.
And not if this helps in any way, but echo $PATH when (imdv_module) appears to the right of the prompt, gives this (I redacted my name): /Users/REDACTEDNAME/learnp/imdb_module/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
While creating the virtualenv you should see it installing setuptools and pip:
$ virtualenv testvenv
New python executable in testvenv/bin/python2.7
Also creating executable in testvenv/bin/python
Installing setuptools, pip...done.
After ensuring the virtualenv is activated you should see your prompt change:
$ . ./testvenv/bin/activate
(testvenv)$
Now you can confirm the paths to python and pip, which should be in the virtualenv:
(testvenv)$ which python
/private/tmp/testvenv/bin/python
(testvenv)$ which pip
/private/tmp/testvenv/bin/pip
If you aren't seeing the python and pip locations as being under the virtualenv's directory, then the virtualenv has not been activated.
I would also ensure that, if you're executing the .py file directly (and not with "python foo.py"), that your shebang line uses:
#!/usr/bin/env python
Or even the full path to the virtualenv's python, e.g.:
#!/tmp/testvenv/bin/python
As opposed to, say:
#!/usr/bin/python
The first will search in the current environment, which will be set by the virtualenv activation. The second explicitly points to the virtualenv's `python'.

setting/changing PYTHONPATH in OS Mavericks

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

Categories