I have installed python 2.7 on my computer. I am able to run python from anaconda but not from command line. The command line says python not found.
Also, how can I have two python versions(2.7 and 3.4) at the same time?
run this command line:
ipython myprogram.py
To create a new virtual environment with a specific version of python, using conda:
conda create -n my_env python=3.6
To switch environments:
on osx/linux: source activate my_env
on windows: activate my_env
to exit source deactivate, or deactivate on windows
more details here: managing conda environments
It can be, that two different python versions are running on your pc. Make sure, you have the same version running on anaconda and the command line.
If on windows just install them both and rename the python.exe tot python2.exe for version 2.7 and python3.exe for python 3.
Now you can call python 2 in the command prompt with
python2
and you can acces python3 with
python3
Also make sure your python path is in your environment variables.
Sure, you can have two Pythons installed.
The command line searches the executable you wanted to run (python, in this case) in the current directory and in all directories specified in PATH environment variable.
It depends on OS, but search how to insert the binary directory (where the python executable resides) into the PATH environment variable.
Related
I already install python so is there a need to check this box of " Add anaconda3 to my path environmental variable"
Anaconda Install
If you've already installed a Python version, it's probably on the system path and you can call Python on the command prompt.
Anaconda works differently. You first have to activate an environment (there is at least the base environment), before you can call Python. You have 2 options:
Call Python from the Anaconda Prompt
On the command prompt run conda activate before calling Python
An activated environemnt will change the command prompt to something like
(base) C:\
If you add Anaconda Python to the Path, your previous install is no longer accessible. Anaconda's Python will start, but with a limited set of packages only and e.g. no C-libraries.
There are only a few special use cases where adding Anaconda Python to the Path makes sense.
I want to keep the Mac Python as my main 'python'. The reason for that is the recommendation in Python website here. I also want to add a separate environment for my Python3 (Anaconda).
For doing that I installed the Anaconda Python to get access to Conda and then I made an environment for my Python3 using the following command:
conda create -n py36 python=3.6 anaconda
When I installed the Anaconda python it added this to my .bash_profile file to get access to all conda commands:
# added by Anaconda3 4.4.0 installer
# export PATH="/Users/omidb/anaconda/bin:$PATH"
Now my default python is anaconda python which I don't want to.
How can I have default Mac python as my main python and then when I needed Anaconda, just use source activate py36 ?
UPDATED ANSWER
After testing this, I feel it's appropriate to offer this as a simple solution for using Mac Python as the default and only using Conda Python when desired.
You need to add/move the conda path to the end of your PATH environment via export command. This should allow you to use the Mac Python as the default and only use Anaconda Python after calling source activate py36.
export PATH="$PATH:/Users/omidb/anaconda/bin"
Path Resolution
This solution assumes you have /usr/bin/ (where Mac Python is) already in your PATH. Resolution order should check that directory first assuming it's first in the PATH. Also, this setup does not require symlinks in /usr/local/bin. I am not a fan of manipulating system-level resources for solutions that can be done with user resources (directories).
Default Python Setup
After moving the Anaconda path to the end of your PATH environment variable, you can validate that which python references /usr/bin/python, the location for Mac Python. You will run Mac python by default at the command line.
Running Conda Python
As previously noted, you have to call source activate py36 when you want to use the conda virtual environment. There is no need for adding symlinks to /usr/local/bin as they are already available through ~/anaconda/bin/.
Furthermore, source activate py36 (or any other Anaconda environment), it will add the appropriate environment path for Anaconda python to the beginning of your PATH environment variable, which (referring back to Path Resolution) would be executed when run as python on the command line. You can validate this with which python after running source activate py36. conda also stores the previous path as the environment variable CONDA_PATH_BACKUP.
Deactivating Conda
After running source deactivate, the original path is restored, so you will then be back to running Mac python.
Faced the same problem and question is too old but the simplest of doing this which I found is:
1. Let's check if the default python version is pointing to Conda python
which python - If Conda installation updated to your bashrc or zshrc, it will show that path
Running Command:
conda config --set auto_activate_base false
It's will make sure that when you start the terminal, Conda doesn't get activated as base
Now if you check python --version or which python - It should be pointing to mac default python version
Now whenever you want to use conda, all conda commands are available with conda <command>
Create the virtual env using conda create --name venv and activate it using conda activate <venv_name>
Now, I am able to use different python versions I required with conda and default python version as system default
I have just created a new env with python 3.5 using anaconda (called it python35). My root env points to python 2.7.11. I cant seem to launch ipython with this new env, here is what I did
1. in conda prompt, activate required env: activate python35
2. confirm the version: python --version
3. launch ipython: ipython notebook
4. open a notebook and do: import sys; print (sys.version)
Step #2, returns 3.5 but step #4 always gives me 2.7.11, its like ipython is picking up python version from root env. How do I fix this. Thanks for any help!
This question is related but I have already done what it suggests.
AFAIK, different environments in anaconda (and in venv as well) are activated by prepending env path to $PATH environment variable. It means, that if some file (eg, ipython) is not found in env path (the first entry of $PATH), the system searches for it in consequent entries of $PATH and finds it in root environment (that stays in $PATH). To fix the behavior, you need to install its own copy of ipython to anaconda env:
In command prompt, activate the environment: source activate python35 (or simply activate python35, depending on the OS)
While in environment, issue the command conda install ipython-notebook
An addition to Andrey Sobolev solution,you should switch to conda install ipython for higher python3.x version and log out the environment by Ctrl+D or conda deactivate then activate again
I could confirm the solution above (basically install notebook in active environment) in my case. Two updates from my side:
Since Anaconda 4.4 (?) ipython notebook is now jupyter notebook. So I had to install jupyter instead. I guess, deactivate and activate was required afterwards to get the path variables in notebook updated -> checked by python -c "import sys; print(sys.path)" or corresponding command in notebook.
If you aren't using a isolated anaconda environment, you may eventual calling the notebook package from your native OS-Python installation, instead the desired from Anaconda. Similarly, I had trouble with cmake or cxx, when I try to compile in Anaconda Environment - the diffent root folder might found in linux bash with e.g "which jupyter"
I used Anaconda for python.
python2 is installed in
D:\Python\Anaconda2
python3 is installed in
D:\Python\Anaconda3
python3 is the default.
created two environmental variables with name: python2 and python2 and selected respective python.exe from different folder respectively.
my setup.py supports only python2.
when i run command from cmd python setup.py install it says it does not support.
If I rename D:\Python\Anaconda2\python.exe to D:\Python\Anaconda2\python2.exe and change the environment path file accordingly it works. But I dnt want to change the file name (as it may break other apps, like conda says unable to create process, etc).
Windows 10 Pro, 64 bit.
setup.py location:
E:\Program Files\IBM\ILOG\CPLEX_Studio1251\cplex\python\x64_win64
How to overcome this? Want python2 setup.py install for python2 compiler and python3 setup.py install for python3 compiler, without renaming.
How to install setup.py by running D:\Python\Anaconda2\python.exe?
I'm not sure whether this directly answers your question,but anaconda manages environments for you. Reference
You should be able to type into your Anaconda prompt to create your environment:
conda create --name pyenv python=2.7
and then list your environments:
conda info --envs
and lastly activate your environment python 2 or python 3 environments:
activate pyenv
These separate environments with their own versions of python are saved in the anaconda folder under the envs folder
/Anaconda3/envs/pyenv/python
Here is how you can use both python2 and python3 in windows 10
Install python2 from here https://www.python.org/downloads/
Install python3 from here https://www.python.org/downloads/
Enter windows keys and type "Edit the system environment variables"
Click on Path and enter Edit button.
Add path to both version of python. (Important) Here i have listed python3 and python2 in order because i want to see version 2 by python --version and version3 by python3 --version.
Go to C:\python310 in windows (or whatever the name is for python3) and update executable file to python3 instead of python.
Save and exit.
Now,
python --version // python 2
python3 --version // python 3
If you want it otherwise, update executable file of python2 to python2 instead of python. And update PATH variables to list python2 and python3 in order.
Is there a way to force conda to use the system version of python (along with all of the system libraries) in a given env?
I have conda enabled by default in my shell, which can get a bit annoying, because if I try to run a system python app, it gets a different version of python to what it is expecting (python still defaults to 2.7 on *buntu), and often won't run. I would like the root env of conda to just be a redirect to the system python install.
You need to edit all user shell run commands such as your .bashrc file to prepend the bin directory of anaconda to path export PATH=~/anaconda/bin:$PATH, while in your root run commands append export PATH=$PATH:~/anaconda/bin. In both cases you will have access to the conda command. You can check which python will be run by typing $env python --version. You can also check which other versions would be available and their order of priority (if the other is removed) by using $type -a python. Of course ensure your executable python files have #!/usr/bin/env python and not some other direct route to a python executable. For further info Google BASH Shell look up queries like http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html.
Simply removing the python symlink from ~/miniconda3/bin/ appears to do the job.
$ which python
/home/naught101/miniconda3/bin/python
$ rm /home/naught101/miniconda3/bin/python
$ which python
/usr/bin/python
$ source activate science
discarding /home/naught101/miniconda3/bin from PATH
prepending /home/naught101/miniconda3/envs/science/bin to PATH
(science)$ which python
/home/naught101/miniconda3/envs/science/bin/python
(science)$ source deactivate
discarding /home/naught101/miniconda3/envs/science/bin from PATH
$ which python
/usr/bin/python
So far, this doesn't seem to have caused me any problems. Unfortunately the same doesn't work for ~/miniconda/bin/python3, because conda requires it when switching to other envs that use the same python version. However, that one hasn't caused as many problems in the first place.
If this does cause problems, it's easy enough to undo, just cd ~/miniconda/bin/; ln -s python3 python (or what ever version of python you're using in your conda root env). You may need to activate/deactivate an env to get that version of python back on your PATH.
If you are in (base) or another environment, use conda deactivate, this will exit conda's environment and place you back into the OS environment:
on MacOS
(base) user$ python --version
Python 3.9.12
(base) user$ conda deactivate
user$ python --version
Python 2.7.16
on Linux (with no base python installed)
(base) user$ python --version
Python 3.9.15
(base) user$ conda deactivate
user$ python --version
-bash: python: command not found