How can I manage mutiple python in Ubuntu16.04? - python

  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.

Related

Python modules not importing

New to python and I cannot import modules that I have installed via pip.
For instance, I have installed numpy though cannot import it.
I have a feeling from trying to work this out that it is installing to the wrong directory, or I am calling the wrong version.
$ which python
returns
/usr/bin/python
I am just not sure how to change it so I can access the modules.
First if all, you are installing the packages using pip, which means you install it on python 2 by the default configurations.
The issue you are describing can be caused by several problems:
If you are working with an IDE like pycharm- your project interpeter might by python 3.x. You should change it to python 2 since you used pip and not pip3.
Some newer versions of pycharm are opening virtual environment by default in new projects. This means that if you install packages outside the virtual environment you will not be able to access them. When opening a project, intended of applying the default settings, change the interpreter to your system interpreter, probably your python2.7 in your case.
You are not using an IDE but accecing python from your terminal like so: python3 instead of python.
Hope it helps ;)
From a terminal try:
pip install numpy --user
This install numpy to your home directory. Sometimes this helps compared with installing it without the '--user' flag.
Then:
python
Now you have a python command line. Try:
import numpy
If you don't see any error message, then the install worked. Control-d or typing 'exit()' returns you to your shell.

How I can delete python 3.4 if i use just anaconda python

I have a specific problem with python. I have on my ubuntu two versions python3.4 and python3.6(from anaconda). I want use just anaconda pythoncurrent version
But when i run script i have some problems with another python version
another version
How i can safe delete useless python 3.4.6?
sudo apt-get remove python3.4
anaconda should have set python3 as default python3.6 version
You should probably not delete Python3 from your system, even if you have Anaconda installed, since there might be system software that:
was not tested with subsequent versions of Python;
might struggle finding Python from Anaconda.
What you should do instead is configure your IDE / environment to run Python script with Anaconda, e.g. by setting your PATH variable to point to your anaconda/bin directory or similar.
If you are using PyCharm, as it seems from the screenshot, you could set up your project to run the Anaconda Python without modifying your other command-line settings.

Running different kernels (e.g. Python 2.7) on Juypter

I have Python 2.7.10 (which I just installed) and I recently installed Jupyter. However the only kind of notebook I can create when I open Jupyter is iTorch and Python 3.
How can I create a notebook in Python 2.7?
I'm attempting to complete a TensorFlow tutorial and that is written in Python 2.7.
The best way to run multiple versions of Python is with virtual environments. Then you can run whatever version you like whenever you like. A great way to do this for people who don't want to spend a lot of time working on it (like me) is with Continuum's Anaconda distribution of Python. I couldn't get the hang of virtual environments till this came along.
Before going any further, I recommend uninstalling the Python 2.7 you installed, and reverting to your system's Python as far as possible.
Then install Anaconda. Install the Python 3.5 version since you'll want to use that most of the time. Almost all libraries I use support it now. Google App Engine and TensorFlow are the only things I use that don't.
Now you should be able to create a Python 2.7 environment like so:
conda create -n python2 python=2.7 anaconda
The python2 is just the name; call it whatever you like. The anaconda bit installs the scientific Python stack; you can leave that off if you want a very basic environment.
Creating the environment doesn't start it. Start the environment with:
source activate python2
Add it to the notebook with:
ipython kernelspec install-self --user
Now install TensorFlow using whichever command Google recommends for your system.
Whenever you want to use that environment, do the source activate command again. To leave it, start another environment or type source deactivate. You can make as many environments as you like, with whatever versions of Python and whatever other packages you want.
Read more about managing environments.

How to get two versions of Python to coexist within a virtualenv?

In order to not alter the default python installation, I have a virtualenv set up that automatically runs on startup. I've also set up my profile so that unless a virtualenv is running, pip will not run. I've been using python2.x up to now, and have now installed python3 via homebrew. When I attempt to install a new package via pip3, I get the following error: Could not find an activated virtualenv (required).. However, when I check for the existence of a virtual env by checking the $VIRTUAL_ENV variable, it tells me that a virtual env is in fact running: -bash: /Users/me/virtualenvs/r: is a directory. Do I need to create separate instances of virtualenvs for each version of python? Why will one of my versions of python recognize the virtualenv, while the other will not?
All your virtualenvs are stored in a directory -- /Users/me/virtualenvs in your case. If you go there, you'll notice that it executes totally separate python interpreters in each virtualenv. For example, this is a freshly created virtualenv:
I'd imagine that running pip3 while on the python2.7 virtualenv wouldn't work for that reason.
It seems that there are two different kinds of virtualenvs, one of which is compatible with python2 and one of which is compatible with python3. I'm not sure whether there is a way to get one that is compatible with both, but I went ahead and created a new virtualenv using python 3 (python3 -m venv myPython3Env), and have been able to successfully use that.

How to check that the anaconda package was properly installed

I'm completely new to Python and want to use it for data analysis. I just installed Python 2.7 on my mac running OSX 10.8. I need the NumPy, SciPy, matplotlib and csv packages. I read that I could simply install the Anaconda package and get all in one. So I went ahead and downloaded/installed Anaconda 1.7.
However, when I type in:
import numpy as np
I get an error telling me that there is no such module. I assume this has to do with the location of the installation, but I can't figure out how to:
A. Check that everything is actually installed properly
B. Check the location of the installation.
Any pointers would be greatly appreciated!
Thanks
You can determine which version of python you are running when you get the error by looking at the results of which python from the commandline. It is likely that you are running the system version (although recent versions Mac OS X include numpy in its system python), rather than Anaconda's python distribution. If this is the case, you need to modify your PATH as suggested by Anaconda at the end of the install process. Assuming it was installed in ~/anaconda, you would need to add something like:
export PATH=$HOME/anaconda/bin:$PATH
to your .bash_profile
Though the question is not relevant to Windows environment, FYI for windows. In order to use anaconda modules outside spyder or in cmd prompt, try to update the PYTHONPATH & PATH with C:\Users\username\Anaconda3\lib\site-packages.
Finally, restart the command prompt.
Additionally, sublime has a plugin 'anaconda' which can be used for sublime to work with anaconda modules.
I don't think the existing answer answers your specific question (about installing packages within Anaconda). When I install a new package via conda install <PACKAGE>, I then run conda list to ensure the package is now within my list of Anaconda packages.
you might want to try this:
for anaconda 2 :
export PATH=~/anaconda2/bin:$PATH
for anaconda 3 :
export PATH=~/anaconda3/bin:$PATH
for anaconda 4 :
Use the Anaconda Prompt
and then
conda --version
to confirm that it worked.

Categories