I am currently using the MSYS2 Building Platform which uses an arch shell to allow users to install their packages via the pacman package manager. On this shell, neither $PYTHONHOME nor $PYTHONPATH are set, and issuing which python3 returns /usr/bin/python3. I have used pacman to install a python version I would actually like to be using, which is located in /mingw64/lib/python3.9/. My first approach was to issue:
export PYTHONPATH=/mingw64/lib/python3.9/
exec bash
But this did not change the output of which python3 or which python. I have also tried modifying the PYTHONPATH variable in a virtual environment but this has not worked either. How can I made the adjustments necessary so that the aliases python and python3 automatically point to the desired python installation?
The solution was to find the location of the mingw64 python installation by issuing
pacman -Q1 | grep /mingw64/bin/
and then appending it to $PATH by issuing
export PATH=mingw-w64-x86_64-python:$PATH
and then reloading the python alias paths by issuing
exec bash
Related
I recently installed python3 on my vps, I want to enable it as default, so that when I type
python I get python 3. I think the problem is its installed in /usr/local/bin instead of /usr/bin/ typing python on the terminal access python2 typing python3 returns bash: python3: command not found
Most answers I have seen is a bit confusing as I am not a centos expert.
for a simple fix, you can use alias
add the alias to your .bashrc file
sudo vi ~/.bashrc
then add your alias at the bottom
alias python="python3.9"
So that when you type python you'll get python 3
There're several ways.
First, check if /usr/local/bin isn't in your $PATH variable:
echo $PATH
If it is indeed not there, you may want to add this line to your .bashrc file (assuming you're using bash):
export PATH="/usr/local/bin:$PATH"
This will add /usr/local/bin to your $PATH variable and will make python3 accessible after you relaunch a terminal session.
Now, changing the default python globally might not be a good a idea as there could be system software depending on python command pointing to python2.
What you could do is use PyEnv which will allow you using different python versions on your computer: https://github.com/pyenv/pyenv
I run macOS Catalina with zshell.
Out of the box the os has one python2 and one python3 version in /usr/bin/python and /usr/bin/python3. I have installed a newer python3 via Homebrew. That version is in /usr/local/opt/python#3.8/bin/python3.
I have added aliases to my ~/.zshrc-file so that both python and python3 will launch into the 3.8 Homebrew version.
When using editors (e.g. Atom) that run python scripts by calling python3 this aliasing does not seem to work. I guess this is because it is specific to the terminal shell.
What is a better way of getting my homebrew python3.8 to become the default python on my system?
Don't uses aliases for selecting alternate programs. Use your PATH variable to manage your preferences.
Start by creating a local bin directory if you don't already have one.
mkdir -p ~/bin
Assuming your PATH is already set up to prefer Homebrew versions over system-installed versions, add ~/bin to the front of the path.
# In .bash_profile
PATH=~/bin:$PATH
Now, create a symbolic link ~/bin/python to the desired Python 3 interpreter.
ln -s /usr/local/opt/python#3.8/bin/python3 ~/bin/python
Now when you run python, you'll get your Homebrew python3.8 interpreter. You can still access the system Python 2 with /usr/bin/python when needed. Your editors should also inherit and respect your PATH variable, unless it is configured to use a specific hard-coded path.
Note that Homebrew still(?) links /usr/local/bin/python to its own Python 2 interpreter; I don't recommend changing that to python3, lest other Homebrew-managed programs get Python 3 when they require Python 2, hence the use of ~/bin. (There's still a chance that programs using python via path lookup will assume it is Python 2, but this should minimize the problems.)
I just installed python 3.6.6 on my server as python 3.7 was giving me too many issues. Unfortunately instead of showing up as python3 executable it is only python. here is the results of dir:
aclocal.m4 config.sub Include Mac Modules Programs Python setup.py
build configure install-sh Makefile Objects pybuilddir.txt python-config Tools
config.guess configure.ac Lib Makefile.pre Parser pyconfig.h python-config.py
config.log Doc libpython3.6m.a Makefile.pre.in PC pyconfig.h.in python-gdb.py
config.status Grammar LICENSE Misc PCbuild python README.rst
I have edited the env path to go to this directory
[root#server]# echo $PATH
/usr/src/Python-3.6.6/python:$PATH
and even
/usr/src/Python-3.6.6/
but obviously that wouldn't work because the python3 command doesn't even exist in the directory. I tried renaming python to python3 so it would run it. Currently the server came with python 2.6.6 and I don't want to disturb that version as I only need this for one piece of software. Though I need to use pip3 and if the system can't find python3 then pip3 command is also not found. Would it have to do with this install process?:
./configure --enable-optimizations --enable-loadable-sqlite-extensions
make altinstall
I wasn't sure if the make altinstall was screwing with it but.
python -3 -m ....
Or
py -3 -m ....
Anything wrong these?
[EDIT]
Further to your comment, it sounds like it could be one of the following:
The file you're trying to use is not in the PATH variable.
https://askubuntu.com/questions/60218/how-to-add-a-directory-to-the-path
Python 3.6 is not in its default position after installation or has been renamed after installation.
Where possible, and practicable, if you're unsure, then ALWAYS install with the relevant installation tool, e.g. apt-get. As far as I'm aware, this will automatically add the directory for Python to the $PATH variable. You can also type that variable into the CLI if you really want to manually check it is 100% in the PATH.
What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".
I have 3 versions of python installed on my Mac. 2 of them are through brew i.e. python2 and python3 while the native version is python. the problem is when I put brew install python it installs python2 and not python. (By saying python means the version which runs on putting that command in terminal). What should I do so that if I type python my brew installed python launches.I have my path variables set correctly and the brew installation path is ahead than that of the usr/bin The problem I am encountering is that I have nltk installed through pip, pip2 and pip3 and when I import nltk in python2 and python3 there is no problem but when I do that in python it show no module found.
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.
ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.
if you run python from a terminal and in the python REPL do the following
import sys
print sys.path
you can see to which site-packages locations are pointed.
Other than that you of course have the option to use virtual environments to set up your version of python
if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)
mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python
and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file
Now start a new terminal session and try out python again it should point to brews version
Hope that helps