Editors not honoring Python shell aliases - python

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.)

Related

How to make python 3.9 my default python interpreter on centos

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

change python version in terminal and intelliJ

short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.

Always run python 2.6 instead of 3.4 in centos

I have installed python 3.4 in centos.
So I opened terminal and typed python or idle then always ran python 2.6 not python 3.4.
How can i run python 3.4 instead of 2.6?
thanks you.
Do not replace the default Python! CentOS's system tools such as yum, system-config-* tools and several other things rely on the default Python 2.6 installation. Set up a virtual environment instead, where you can define which is the default version.
virtualenv --python=/usr/bin/python3.4 myenviron
source myenviron/bin/activate
First, look for where your Python 3.4 is located:
$ which python3.4
/usr/bin/python3.4
See if ~/bin (e.g., /home/username/bin) directory is in the PATH environment variable:
$ echo $PATH
/home/username/bin:/usr/local/bin:/usr/bin:/bin
If not, add the bin directory to PATH, ideally within your ~/.bashrc file. Then create a symlink pointing to the Python interpreter under the bin directory:
$ ln -s /usr/bin/python3.4 /home/username/bin/python
This way, when you type python on the command line, the interpreter specified will be launched.
I am on Debian/Wheezy, so the detail might be slightly different, but a similar approach should work.

"Proper way" to manage multiple versions of Python on archlinux

So I have read this - https://wiki.archlinux.org/index.php/Python
And it is clear from this wiki that I can install Python 2.7.2 via
pacman -S python2
Is it reasonable for me to create a symlink to python2
ln -s python2 /usr/bin/python
if I don't forsee myself switching to python 3.0 any time soon? Or is there a better way of managing multiple python versions like what I usually use on a debian system (update-alternatives --config python) or on a mac os x system (python select)?
CLARIFICATION:
What I am trying to find out is - what is the "best practice" of managing various python versions on an archlinux system?
I am new to archlinux but familiar with ubuntu, debian and mac os x
I would argue you shouldn't create any symlinks like this at all. Especially if you are going to distribute some of your python code, you should not assume a user has python2 or python3 at /usr/bin/python.
If your script requires python2, just use:
#!/usr/bin/env python2
If your script requires python3, use:
#!/usr/bin/env python3
This way your scripts will work fine even through updates to Python. It will also be much more clear what version your script actually needs.
Most unices already have a /usr/bin/python. Overwriting that one is a bad idea, as this is the Python version used by all packages in the system, and changing that one may break them. When installing the Python 2.7 package the executable should be installed as /usr/bin/python2.7 (if not I would claim Archlinux is broken) and it's better to use that when you want to run Python 2.7.
Archlinux is a bit special, since it will use /usr/bin/python for Python 3, despite the default executable name for Python 3 being /usr/bin/python3. This is confusing and can be seen as a bug, but it does mean you can't use that symlink for Python 2, as any other Archlinux script that uses Python 3 will almost certainly break if you do.
So where on other Unices, symlinking /usr/bin/python to Python 2.7 is a bad idea, on Archlinux it is a terrible idea. Instead just install all version you need and call them with /usr/bin/pythonX.X.
There is a nice project on github what helps you with it's called pyenv
What helps you to manage multiple python instances
As others have said, the short answer is "don't do this, it will most likely break things on your system", however, if you mostly use Python 2 you can still set your personal default in your shell (and still have the option of switching to Python 3 at any time). To do this, first become root and install python2-virtualenv:
# pacman -S python2-virtualenv
Then create a virtual environment that uses Python 2 (this will automatically install Python, setuptools, wheel, and pip in the environment):
$ virtualenv -p /usr/bin/python2 --system-site-packages ~/env # (Or wherever you want your environment to live)
If you only want to use locally installed packages (eg. packages you install with pip and not the ones installed by pacman) remove the --system-site-packages option when creating your environment.
Now in your ~/.bash_profile or ~/.profile (or whatever your preferred shells configuration file is), set something like the following:\
source ~/env/bin/activate
This will activate the virtual environment, making your default version Python 2.
This could still break anything that is launched in a shell, but it's not likely that anything will be unless you're explicitly running it from a shell, at which point you can turn the virtual environment off by running:
deactivate
or simply manually run Python 3.
I just stumbled over this post, no necro-bumping intended but I was wondering nobody mentioned virtualenvs. I'm using ArchLinux as well and I use the python packages virtualenv and virtualenvwrapper to create multiple python environments. You can reference to the python 2 or python3 binaries in /usr/bin/ to determine the python version used in the virtual environment.
The benefit is that packages installed in a virtual environment don't mess with the python the system is using and there are many ways to automate project handling.
I know this may be a very old answer, but it took me two days to solve the problem so I'm going to share.
The proper way to manage python versions in your system to work on different projects without them driving you crazy is to use pyenv and its plugins pyenv-virtualenv and pyenv-virtualenvwrapper as described by Henrique Bastos into this blog post. Notice that this way to work is kinda platform independent, since pyenv is a python package and it can be run quite similarly on Windows, Linux and Mac OSx.
The problems start with Arch Linux. The OS doesn't provide a pacman version of pyenv, so you have to install it cloning it from github as described in the installation section of the release. The installation process is the same both for pyenv-virtualenv and pyenv-virtualenvwrapper. Notice that the shell initialization configuration may be different, in my case it didn't work for ~/.bash_profile, but worked for ~/.bashrc .
Running pyenv is not straightforward if your installation is very fresh like the one I'm setting up in these days, since pip requires openSSL and even if you install it via pacman, pyenv doesn't see it. So, if you want to install an older version of Python (namely 3.4.3), you will find the shell complaining about you haven't installed the openSSL plugin, even if you have it. To be honest, I didn't have the right packages the first time I tried to install it; you have to download the following packages
sudo pacman -S openssl
sudo pacman -S openssl-1.0
sudo pacman -S python-pyopenssl
sudo pacman -S python2-pyopenssl
The way I solved the problem is to add the flags as described in the pyenv installation FAQs: that solution eventually led me to install the python version I wanted:
LDFLAGS="-L/usr/lib/openssl-1.0" \
CFLAGS="-I/usr/include/openssl-1.0" \
pyenv install -v 3.4.3
To avoid to go on the FAQs page everytime you want to renew the python installation environment, you can add an alias in ~/.bashrc or whatever is you shell as follows:
echo alias pyenv='LDFLAGS="-L/usr/lib/openssl-1.0" \
CFLAGS="-I/usr/include/openssl-1.0" \
pyenv' >> ~/.bashrc
In this way you can install python properly with a clean pyenv syntax, and manage it via its plugins in the same way (since the syntax is pyenv [COMMAND] [OTHERSTUFF]).
No, there is no better way to do this. The python symlink is part of the Python 3 package.
I guess changing this link won't break anything for now but it might be possible that some packages will depend on it in the future.

How to make python_select work for '$>python' command?

I installed a couple of pythons in different versions with macports, and the apple python 2.6 is also working. Now I need to run a program which requires MySQLdb package support in python, and this package was installed to the python I installed by macports. The program tells me that there is no MySQLdb installed, so I guess it is the apple python working for that program.
I searched for some help and found python_select for switching between pythons. However after the command
$>sudo python_select python25
told me that it selected the version "python25" for python, when I type
$>python
it is still apple python 2.6 that launches.
The question is that how can I make python25(the one with MySQLdb) work for the program rather than apple python?
Another important thing, the program is NOT a .py file and needs to be compiled before running. So do I need to re-install this program? My Mac OS version is Snow Leopard 10.6.
Any answer is appreciated.
By default, MacPorts installs user programs (or links to them) in /opt/local/bin. The MacPorts select_python command selects which python instance is linked to /opt/local/bin/python. It has no effect (nor should it) on what Apple installs in /usr/bin, which is where the Apple-supplied python and python2.x commands are.
To invoke the MacPorts python2.5, you either need to ensure that /opt/local/bin precedes /usr/bin on your shell $PATH (you can do this by modifying your .bash_profile or other shell initialization script) or you can simply invoke the desired python with an absolute path reference:
$ /usr/bin/python your-program.py
to use the Apple-supplied default python;
$ /opt/local/bin/python your-program.py
to use the version selected with python_select, or:
$ /opt/local/bin/python2.5 your-program.py
to explicitly select the MacPorts 2.5 one.
EDIT:
To modify your search PATH to use MacPorts, add this line to .bash_profile:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
First, I am not sure with Mac, coz I never use it before.
but in Linux, when I do whereis python
It will show like /usr/bin/python /usr/local/bin/python ....etc
in my .bashrc file, I just export PATH=/usr/local/bin:/usr/bin:$PATH when I want /usr/local/bin more priority
or you still can run like
/usr/bin/python yourpython.py
or
/usr/local/bin/python yourpython.py
depends on your python install locations
just my 2 cents. sorry if my answer dont make you any helps.
'python' in the Mac is just a link. Do a 'which python', 'cd' to the directory in which 'python' resides, and then do an 'ls -a py*'. You shall see where python is pointing too. If you want that python to point to your different version of python, just make it link to the right version.

Categories

Resources