Python2 and Python3 both in windows 10 - python

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.

Related

Cannot activate correct Python in remote computer

I have moved a virtual environment onto a remote computer. I am trying to activate python 3.8 but not having much luck.
The virtual environment I'm using was created using venv. But I created it on my Mac and then moved it onto the target Linux computer. The following demonstrates that the Linux computer has python 3.8
kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate_this.py pip pip3.8 python3
activate.csh easy_install pip2 python python-config
activate.fish easy_install-2.7 pip2.7 python2 wheel
Activate.ps1 easy_install-3.8 pip3 python2.7
I activated the virtual environment with the following commands:
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
However, the following command shows that python 2.7 was activated
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python --version
Python 2.7.13
Further, when I ran one of my programs I got a syntax error that only python 2.7 would throw:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3 fix_mistakes.py
File "fix_mistakes.py", line 113
p = print
p (f"{round(c - b,0)} seconds")
SyntaxError: invalid syntax
Even when I run the command python3, it activated python 3.5 as demonstrated by the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
I even think that the computer is not even using my working environment but the default python interpreter due to the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ which python
/usr/bin/python
I would think that the intepreter would be in my virtual environment which would be:
/venv_byu/bin/python3
#####UPDATE
I was able to install venv on the linux but I'm still activating python 3.5.3
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ which python
/home/kylefoley/byu_corpus_small/venv_byu/bin/python
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ python --version
Python 3.5.3
Apparently the environment I downloaded did not have 3.8
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate.fish easy_install-3.5 pip3 python
activate.csh easy_install pip pip3.5 python3
Now, I just need to figure out how to get 3.8
Virtual environments are not portable. You should create a new virtual environment on the destination computer and populate it with the packages you need. Listing them with pip freeze on your local computer or manually enumerating them in requirements.txt are two common approaches.
The standard virtual environment shipped with Python actually hard-codes the path of the virtual environment, so you can't even rename the directory locally, much less copy it to a different directory structure on a different computer.
The activate command needs to be run with source for various reasons, but that also means that it is not very robust against failures. For example, it can throw an error or fail silently, but still update your prompt so that it appears as if the virtual environment was successfully activated.
To create a new virtual environment on a computer where you have Python 3.8 installed as /usr/local/bin/python3.8, you can run
/usr/local/bin/python3.8 -m venv venv_byu
You then need to activate this environment, and pip install or otherwise populate it with the libraries you need.
pip freeze will list the exact versions of all installed packages, so it is more precise in getting exactly the same version of everything. If you manually list packages in requirements.txt, you don't have to specify a precise version of anything, and you can leave out packages which are pulled in as dependencies of the packages you actually specifically depend on.

Rename python 3.7 executable without breaking stuff

I have renamed python.exe to python37.exe to avoid conflict with other versions. It works for running python, but if I run pip37.exe (located in /Scripts) I get the following error:
Fatal error in launcher: Unable to create process using '"c:\python37-32\python.exe" "C:\Python37-32\Scripts\pip37.exe"
Is there a way to keep python.exe renamed to python37.exe, but keep all python tools working?
This sounds like A BAD IDEA.
There are tools designed to help you manage exactly this sort of thing. The best of which imho is pyenv: https://github.com/pyenv/pyenv
It's quite simple to install. It takes a bit of getting used to – wrapping your head around virtual environments – but it makes it all so much easier to work with ultimately.
E.g. On my system I have the following versions of python:
pyenv versions
system
2.7.10
* 3.5.6 (set by /Users/.pyenv/version)
3.5.6/envs/core4
3.6.4
3.6.4/envs/core5
core4
core5
The one with the asterisk is currently the global version, which will be the one used from any default shell. I can change that using pyenv global 3.6.4 for example. I can also create virtual environments. E.g. core4 and core5 are virtual environments I created for specific projects. Each of these will have their own different libraries installed by pip install and differing python versions. You can activate a virtualenv for a given shell session e.g. pyenv activate core5.
And if you're thinking "what on earth does this have to do with Windows", look here: https://duckduckgo.com/?q=Windows+Subsystem+for+Linux&atb=v93-1__&ia=web and here: http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows/
On Windows Python installs PyLauncher. You don't need virtual environments or renaming tricks. py.exe is in the standard Windows path, has command line switches to pick the Python version to use, and enables using "shebangs" to specify what version of Python to run for scripts:
py script.py # Run the latest Python installed (or specified by PY_PYTHON environment variable).
py -2 script.py # Run the latest Python 2 version installed.
py -3 script.py # Run the latest Python 3 version installed.
py -2.7 script.py # Run the specific Python version.
py -2.7-32 script.py # Run the 32-bit specific Python version.
py -0 # List Python versions installed.
Scripts can use shebangs similar to Linux:
#!python2
#!python3
#!python2.7
#!python2.7-32
To run pip with a specific version:
py -2.7 -m pip install ...
If you need still would like a virtual environment with a specific Python version, you can specify the version (e.g. -3) and use:
py -3 -m venv <my_env_name> # to create an virtual environment
<my_env_name>/scripts/activate # to activate that environment
Activation adds the virtual environment to the path, so python (not py) will run in that environment. the Scripts directory in that environment will also be added to the path, so pip can be run directly as well to install packages in that environment.

Python not running from command line but running from anaconda shell

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.

How do I make Python 3.5 my default version on MacOS?

I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?
Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.
So for Idle, you need to type idle3 in the terminal in order to use it with Python 3 and idle for using it with Python 2.
Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3 when you want to use Python 3 and python when you want to use Python 2.
It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin not MacOS default usr/bin.
.bash_profile
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
Can also create aliases for both.
alias py2='python2.7'
alias py3='python3.6'
Source the file to ensure it takes effect for the current session
source ~/.bash_profile
Homebrew install and setup etc...
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update
brew upgrade --all
brew cleanup
Python3 install
brew install python3
Next
pip3 install virtualenv
Next
pip3 install virtualenvwrapper
When all is finished python3, pip3, virtualenv, and virtualenvwrapper.sh will all be in usr/local/bin.
Result
Every time I install anything or use commands like mkvirtualenv Python 3 is used by default.
You can use the python3 command (instead of using python), or you can simply uninstall the 2.7 version if you don't use it
If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3 to launch IDLE.
I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.
You can switch to any python version in your project by creating a virtual environment.
virtualenv -p /usr/bin/python2.x (or python 3.x)
In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x
Do right thing, do thing right!
Open your terminal,
input python -V, It likely shows:Python 2.7.10
input python3 -V, It likely shows:Python 3.7.2
input where python or which python, It likely shows:/usr/bin/python
input where python3 or which python3, It likely shows:
/usr/local/bin/python3
add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.
Quit the terminal.
Open your terminal, and input python -V, It likely shows:
Python 3.7.2
Note, the ~/.bash_profile under zsh is not that ~/.bash_profile.
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
Hope this helped you all!
By typing python, you are actually referring to a link.
You will find its location with $ which python. In my case it was /usr/local/bin/python. go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are
identical to the 2.7 files in the same folder.
Then duplicate the 3.5 files and rename them to what you just deleted.
This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.

Allow use of system python in conda env?

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

Categories