How can I use both Anaconda versions (2.7 & 3.5)? - python

I was using the Anaconda 3.5 distro in a Windows 10 machine. Due to dependencies in libraries that I want to work with, I had to have the 2.7 version installed as well.
The good news is that the libraries I needed can now work with the 2.7 version smoothly and Visual Studio 2015 automagically detected my new Python environment.
The problem comes when using the command line. Upon issuing the command
conda info --envs
I get
root * C:\Users\norah\Anaconda2
i.e. a single environment (to my understanding and search so far, according to this I should see two envs listed). This means I can't use conda to acquire new packages for my Python3.5 installation, at least not at the command line as I used to since conda only refers to Python2.7. The same goes for the GUI version, Anaconda navigator (I'm not very fond of the GUI version but I tried it out).
There's also no way of launching python3 from the command line since
$python
always fires up python2.7 and issuing $python3 or $python3.5 in the command line doesn't seem to work (nor would adding the path of python3 to the system since the actual executable has the same name as python2 i.e. python.exe)
Is my system taken over by Python2.7? Is anyone here using them both smoothly and if so could you please elaborate on how to do that? Was it a "no no" move to install both versions of Anaconda?

You can make Python 3.5 environment with your Anaconda 2.7:
conda create -n py35 python=3.5
Now, activate it:
activate py35
Finally you can install the desired packages:
conda install numpy
or, the whole anaconda:
conda install anaconda
The advantage of this approach is that you can also create Python 3.4 or 3.6 environments. Furthermore, you can create environments with different combinations and versions of libraries.
Actually, it makes sense to create a new environment for each larger project.

I also despise the virtual environment switch that Anaconda tries to force on us. I prefer to have both executables always instantly available from the command line. I'm pretty sure I had this working on a Windows machine once:
Install Anaconda2 and Anaconda3 to the C:\ drive as "C:\Anaconda2\" and "C:\Anaconda3\" respectively.
Edit your "Path" environment variable (Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables) and make sure that "C:\Anaconda2;C:\Anaconda2\Scripts;C:\Anaconda2\Library\bin" is in front of "C:\Anaconda3;C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin".
Copy and rename the file "C:\Anaconda3\python.exe" to "C:\Anaconda3\python3.exe".
Copy and rename the file "C:\Anaconda3\Scripts\conda.exe" to "C:\Anaconda3\Scripts\conda3.exe"
Copy and rename any other scripts you might use in "C:\Anaconda3\Scripts\", such as "pip.exe" to "pip3.exe", etc.
Now, when you type "python" or "conda" at the command line you will get the python2 version, and when you type "python3" or "conda3", etc. at the command line you will get the python3 version.

The best way to use both Python 2.7x and Python 3.5x together is Jupyter Notebook.
http://jupyter.org/
You will be able to work on your browser with IPython Notebook style interface which is great for working with scripting languages.
I found some of these videos on YouTube very informative:
1) https://www.youtube.com/watch?v=HW29067qVWk
2) https://www.youtube.com/watch?v=e9cSF3eVQv0
Besides Python 2.7 and 3.5, there are a bunch of other languages that you will be able run with your Jupyter Notebook. The various Kernels are available in this link below:
https://github.com/jupyter/jupyter/wiki/Jupyter-kernels
After installation, as you start your Jupyter Notebook, your browser will open up a new page showing your Home Directory. Among the 4 tabs (Files|Running|Clusters|Conda):
1) The Files tab shows all the files in your Home Directory.
2) Running tab shows all the Notebooks that are running.
3) Clusters tab is provided by IPython parallel.
4) Conda tab is where you need to add the Python version 3.5 (and other languages if needed) to your Jupyter Notebook (If Python 2.7 is default).
If you are interested to try C++ with your Jupyter Notebook, there are a couple of Kernels available.
1) JupyRoot - https://github.com/root-mirror/root/tree/master/bindings/pyroot/JupyROOT
2) Cling - https://github.com/root-mirror/cling

Related

Is there a way to create a virtual environment with an earlier version of python?

I have python 3.8 installed on my pc, but i need 3.7 for a specific task. When i try to set up a virtual environment via
virtualenv -p "my/path/to/python37.exe"
it calls the installer, so i am to install py37 manually, but then it ends up with an error:
Error 0x80070666: Cannot install a product when a newer version is installed
Is there a proper way to implement such a thing?
Thanks!
Have you tried anaconda or miniconda (the lighter version of anaconda)? Having more python versions in different environments and switching between them is quite easy.
I haven't installed Anaconda. If you want to have multiple different environments with pure Python you can do it like this:
1) Install the python versions that you want with the exe installer, ie Python 2.x.x, Python 3.7.x, Python 3.8.x etc, maybe inside a common folder like C:\Python
2) Then edit the System Variables path and pinpoint to the folder for the version you want to create a virtual environment. You need two entries here, one to python folder (for the python.exe) and one to the Scripts folder (for the pip.exe)
3) Open command prompt and hit python. You ll see that it's showing the apporpriate version. Install the virtual environment with "python -m venv name_of_env_you_want"
4) If you want to create a virtual environment with a different version change the paths

why Anaconda does not recommend to set path environment variable

Anaconda3 5.3.0 64bit.
During installation process, I check the option to add Anaconda in my PATH environment variable. Then it does not recommend me to do that. It says:
Instead, open Anaconda with the Windows Start menu and select "Anaconda (64-bit)". This "add to PATH" option makes Anaconda get found before previously installed software, but may cause problems requiring you to install and reinstall Anaconda.
What does it really mean? I would like to add it to PATH because I would like to work also by means of the command line interface, but I would not like to have problems.
If Python is already installed and is being used by other programs, setting the conda path again can lead to unwanted conflicts.
For this reason Anaconda provides a separate cmd line/Powershell interface called Anaconda prompt which sets the PATH as long as the shell is open.
If you're installing Python for the first time using Anaconda, there shouldn't be any problems.
You can create environments using conda if you want to work with different python versions. Anaconda will automatically set the path for both scenarios once the environment is activated.

Activating conda environment without separate Python version in IDEs

I recently created a conda environment (in Ubuntu 16.04), which is working great from the command line. I can activate it with no problems, the packages are properly separated, etc.
However, because it is running the same version of Python as the Anaconda installation (Python 3.6.1), it has not created a separate interpreter. Within the /anaconda3/envs folder there are no other folders. This makes it difficult when I want to use editors (such as vscode and Pycharm) because they cannot find the path to some of the packages I have installed in the environment. I would love to start using the great debugging features in Pycharm but without a separate interpreter I have no idea where to even start. Is there a way to initialize the interpreter in the correct environment?
When I run conda info --envs, this is what I get:
# conda environments:
#
chatbot /home/bradley/.conda/envs/chatbot
tensorflow /home/bradley/.conda/envs/tensorflow
tf_testing /home/bradley/.conda/envs/tf_testing
root * /home/bradley/anaconda3
If I try to run the Python in these directories (a small executable script exists), it doesn't include all the packages. Should I maybe add an environment variable? If so, what would I write?

Anaconda3 2.4 with python 3.5 installation error (procedure entry not found; Windows 10)

I have just made up my mind to change from python 2.7 to python 3.5 and therefore tried to reinstall Anaconda (64 bit) with the 3.5 environment. When I try to install the package I get several errors in the form of (translation from German, so maybe not exact):
The procedure entry "__telemetry_main_return_trigger" could not be found in the DLL "C:\Anaconda3\pythonw.exe".
and
The procedure entry "__telemetry_main_invoke_trigger" could not be found in the DLL "C:\Anaconda3\python35.dll".
The title of the second error message box still points to pythonw.exe. Both errors appear several times - every time an extraction was completed. The installation progress box reads
[...]
extraction complete.
Execute: "C:\Anaconda3\pythonw.exe" "C:\Anaconda3\Lib_nsis.py" postpkg
After torturing myself through the installation I get the warning
Failed to create Anaconda menus
If I ignore it once gives me my lovely error messages and tells me that
Failed to initialize Anaconda directories
then
Failed to add Anaconda to the system PATH
Of course nothing works, if I dare to use this mess it installs. What might go wrong? On other computers with Windows 10 it works well.
P.S.: An installation of Anaconda2 2.4 with python 2.7 works without any error message, but still is not able to be used (other errors).
Finally I have found the reason. So, if anybody else has this problem:
Here the entry points are an issue as well and Michael Sarahan gives the solution. Install the Visual C++ Redistributable for Visual Studio 2015, which is used by the new version of python, first. After that install the Anaconda-package and it should work like a charm.
You can also use your standard Anaconda installation and just create an environment based on 2.7 / 3.4 etc... Anaconda will download and install all compatible packages from the repository so you have a complete installation.
conda create -n py27 python=2.7 anaconda
conda create -n py34 python=3.4 anaconda
To use the new environment (in Windows), you can point your IDE to the required environment folder:
C:\Anaconda3\envs\py34\python.exe
On the Windows command line, you just need to type:
activate py34
Documentation:
http://conda.pydata.org/docs/py2or3.html
My recommendation would be to reinstall your Anaconda version with Python 2.7 that you previously had working, then just add a new environment for Python 3.x and use that as you need.
I had the same problem, then I Installed "for all users": solved.
Much easier than the links provided for a beginner as I am.
For the problem "Failed to create Anaconda menus", it may because:
This might happen if PATH is too long already. How long is your PATH environment variable? Windows limits this to 1024 characters. There are some workarounds go shorten it here: How do you avoid over-populating the PATH Environment Variable in Windows?
This is other people's question and solution
https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/mjrbE6rKyi8
When I shorted the environment path, it works!
If you are getting errors like:
Failed to create Anaconda menus
Failed to initialize Anaconda directories
Failed to add Anaconda to the system PATH
just ignore them while installation and when installation is done look for the directory "anaconda3" is installed and correct the path accordingly in environment variables path.
In my system, path was set "C:\Anaconda3" but actually it was installed at "C:\ProgramData\Anaconda3". You have to change all 3 path entries for anaconda3 and then try to run "jupyter notebook" in CMD.
If you are using windows, launch the command prompt as administrator and execute the following commands
"C:\ProgramData\Anaconda3\pythonw.exe" -E -s "C:\ProgramData\Anaconda3\Lib_nsis.py" addpath
"C:\ProgramData\Anaconda3\pythonw.exe" -E -s "C:\ProgramData\Anaconda3\Lib_nsis.py" mkdirs
"C:\ProgramData\Anaconda3\pythonw.exe" -E -s "C:\ProgramData\Anaconda3\Lib_nsis.py" mkmenus
Don't forget to change the path to the path in your system. Before running this commands there will not be any Anaconda Navigator app in your start menu. After executing this commands make sure Anaconda Navigator app is available in the start menu.
Change the directory. It works
C:\miniconda3

Using (Ana)conda within PyCharm

I've got Pycharm 4 running on my Linux (Ubuntu 14.04) machine. In addition to the system python, I've also got Anaconda installed. Getting the two to play nicely together seems to be a bit of a problem... PyCharm provides some interesting integration for virtualenvs and pip, but the Anaconda Python distribution seems to prefer using its own conda tool for both activities.
Is there a relatively simple/painless way to be able to use conda in conjunction with PyCharm? Not just as an alternative interpreter i.e. point PyCharm at the Anaconda Python binary for a project interpreter, but to be able to create, source/activate and deactivate virtual envs, add/remove packages in those virtual envs, etc.
Or am I going to have to choose between using Anaconda (and having a more recent and up-to-date python than may come with the system), and being able to use PyCharm's features to their fullest extent?
I know it's late, but I thought it would be nice to clarify things: PyCharm and Conda and pip work well together.
The short answer
Just manage Conda from the command line. PyCharm will automatically notice changes once they happen, just like it does with pip.
The long answer
Create a new Conda environment:
conda create --name foo pandas bokeh
This environment lives under conda_root/envs/foo. Your python interpreter is conda_root/envs/foo/bin/pythonX.X and your all your site-packages are in conda_root/envs/foo/lib/pythonX.X/site-packages. This is same directory structure as in a pip virtual environement. PyCharm sees no difference.
Now to activate your new environment from PyCharm go to file > settings > project > interpreter, select Add local in the project interpreter field (the little gear wheel) and hunt down your python interpreter. Congratulations! You now have a Conda environment with pandas and bokeh!
Now install more packages:
conda install scikit-learn
OK... go back to your interpreter in settings. Magically, PyCharm now sees scikit-learn!
And the reverse is also true, i.e. when you pip install another package in PyCharm, Conda will automatically notice. Say you've installed requests. Now list the Conda packages in your current environment:
conda list
The list now includes requests and Conda has correctly detected (3rd column) that it was installed with pip.
Conclusion
This is definitely good news for people like myself who are trying to get away from the pip/virtualenv installation problems when packages are not pure python.
NB: I run PyCharm pro edition 4.5.3 on Linux. For Windows users, replace in command line with in the GUI (and forward slashes with backslashes). There's no reason it shouldn't work for you too.
EDIT: PyCharm5 is out with Conda support! In the community edition too.
Change the project interpreter to ~/anaconda2/python/bin by going to File -> Settings -> Project -> Project Interpreter. Also update the run configuration to use the project default Python interpreter via Run -> Edit Configurations. This makes PyCharm use Anaconda instead of the default Python interpreter under usr/bin/python27.
as per #cyberbikepunk answer pycharm supports Anaconda since pycharm5!
Have a look how easy is to add an environment:
Continuum Analytics now provides instructions on how to setup Anaconda with various IDEs including Pycharm here. However, with Pycharm 5.0.1 running on Unbuntu 15.10 Project Interpreter settings were found via the File | Settings and then under the Project branch of the treeview on the Settings dialog.
this might be repetitive.
I was trying to use pycharm to run flask - had anaconda 3, pycharm 2019.1.1 and windows 10. Created a new conda environment - it threw errors.
Followed these steps -
Used the cmd to install python and flask after creating environment as suggested above.
Followed this answer.
As suggested above, went to Run -> Edit Configurations and changed the environment there as well as in (2).
Obviously kept the correct python interpreter (the one in the environment) everywhere.

Categories

Resources