Confusion using path, multiple versions of python on mac osx - python

I'm new to using terminal for python. And I am struggling to understand this environment after installing sublimetext to test and develop codes conveniently.
Below is one question. If I command the following,
echo $PATH
Returns,
/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
It seems like I have python 3.5 and I want to completely remove it since I only want 2.7 on my osx. If I check the version of python using -V, it returns 2.7. Further from the following command,
open ~/.bash_profile
Returns,
export PATH=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
export PATH=/usr/local/bin:$PATH
export PATH="$HOME/.rbenv/bin:$PATH"
export PATH=/usr/local/bin:$PATH
export PATH
export PATH=/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I don't seem to have 3.5 anywhere. I was recommended to check path environment to remove whatever bits left of 3.5 python but I can't. I seem to be missing a lot of something I can't figure on my own. I would appreciate some experts' advise..
Thanks in advance.

There is no need to uninstall Python 3; it coexists with Python 2 with no interference.
The last export PATH=... statement compiletely overrides all of the previous ones. You don't need to explicitly export PATH at all from your personal settings anyway, because the system alseady does this. The multiple additions of /usr/local/bin are obviously redundant.
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
export PATH="$HOME/.rbenv/bin:$PATH"
would fix the problems and allow the Ruby uninstaller to remove itself at a later time (so I left in the useless export there in case it looks for that when removing itself).

Related

MacOs Monterey Python3 as default

I am currently trying to change my default Python version to Python3. This proves to be harder than expected.
I have already tried the following things:
I have tried to change the alias by doing alias python python3 but this seems to just change it for the running Terminal session
I have installed Python3 again with Homebrew and tried to change the directory within the zshellruncommand by defining it myself and editing it in vim
to be honest I don't really understand the process and just followed along with a tutorial but it didn't work.
I tried the command ln -s -f /opt/homebrew/bin/python3 /usr/bin/python to change the directories but it returned ln: /usr/bin/python: Operation not permitted
to deal with this problem I have already given the Terminal full access to the local harddrive but it is still not working properly
Hoping for some advise,
thanks in advance
First make sure the installed version is linked:
brew link python
then on your .bash_profile or .bashrc (.zshrc if you're using zsh) write this:
export PATH="/usr/local/opt/python#X.Y/libexec/bin:$PATH"
change X and Y to your python version
I think it's easier to just change your user shell environment instead of changing the system level environment.
vi ~/.bash
Then type
alias python=python3
Save your file by pressing Esc -> type :wq
Update your shell environment
source ~/.bash
Check your Python version.
python -V
Final result
Python 3.9.10
This works in my past few versions MacOS.

Configuring the PATH variable in VS Code

I am using Microsoft Visual Studio Code 1.30.2 on macOS Mojave 10.14.3. In the settings of VS Code, I have required it to use the external Terminal.app application on macOS. I also have an Anaconda distribution of Python installed.
Note that the PATH variable here reads:
~ » $PATH
bash: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/anaconda3/bin:/anaconda3/condabin: No such file or directory
The order above is different from what I get if I fire up the regular Terminal app and get the PATH.
~ » $PATH
-bash: /anaconda3/bin:/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin: No such file or directory
What this effectively does is switch the versions of Python when I type in python in the two cases. In the regular terminal, I get Python 3 right away, whereas in VS Code, I am faced with Python 2.
How do I change the PATH in VS Code so that whenever I type the words python, I am always given Python 3 instead of 2?
P.S. Please note that while editing Python code in VS Code, I can always select which interpreter I want to use, but that does not change the situation with the terminal underneath.
I had the same problem, and I have a workaround solution.
I just add one line
export PATH="/Users/username/anaconda3/bin:$PATH" into .bash_profile or .zshrc
The orifinal $PATH in vscode is
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin
And then my $PATH in vscode become /Users/username/anaconda3/envs/py36/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin
Please give it a try. I wish the workaround might help. Thanks :)
It sounds like your Workspace setting python.pythonPath is set explicitly to the /usr/bin. Set that to match your preferred $PATH and you should be good to go.
Go to Preferences in VS Code, search for python.pythonPath, switch to Workspace, and set as desired.

change python directory from conda

Recently, I installed miniconda, and because of which, my default python directory seems to have changed to,
/home/user/miniconda3/bin/python
Also, the default version seems to have changed to python 3.7 instead of python 2.7
And I cannot seem to install various libraries like tensorflow.
How do I get my python back to normal?
The miniconda installation has properly added itself to your PATH. You can check whether /home/user/miniconda3/bin is listed when you run
echo $PATH
in a terminal. If so, it most likely added some lines to the bottom of your ~/.bashrc file. Simply remove these lines yourself, and your PATH should be back to normal (save the file and open up a new terminal), meaning that typing e.g. python will give you the system Python.

How can I make my PATH more succinct in ZSH?

I am using ZSH. I was having trouble running the Anaconda package manager commands in my terminal. I found that I could add the bin to my PATH using this code:
export PATH="$HOME/anaconda3/bin:$PATH"
That worked. All of the Anaconda and associated commands work and it left me with this PATH:
/Users/USER/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
But the PATH variable would reset each time I closed the terminal window. After some research, I figured out how modify my .zshrc to make it permanent. I simply pasted this into my .zshrc:
export PATH=/Users/USER/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
All the commands work and are permanent, but aren't I setting myself up for failure down the road? Everything in the PATH, minus the "/Users/USER/anaconda3/bin" were set automatically. What happens when I update Python, for example? How can I include the "USER/anaconda3/bin" portion of my PATH in my .zshrc without being so explicit?
Your PATH concern is not zsh specific. You could organize things differently.
For example, you could decide to add a directory $HOME/bin/ early in your PATH and put inside that $HOME/bin/ symbolic links to the executables (or scripts) that you want to use. BTW, I recommend to have a short PATH (containing some $HOME/bin/ etc...) since it is more efficient and less messy to understand.
What happens when I update Python, for example?
Let's suppose that Python is installed in your system in /usr/bin/python (and that you use some Linux distribution -or some other Unix- with a good package manager dealing with that;on MacOSX consider homebrew). When that file /usr/bin/python is updated, any future exec of it (e.g. by some shell) will use the new version. Read carefully execve(2).
If ou have several Python-s and the one you want to use is under /Library/Frameworks/Python.framework/Versions/3.6/bin and you upgraded it to some Python 3.7 installed under /Library/Frameworks/Python.framework/Versions/3.7/bin you would need to change your PATH.
If you followed my suggestion, you would (for example) just have a symlink from $HOME/bin/python to /Library/Frameworks/Python.framework/Versions/3.6/bin/python and you would upgrade that symlink when installing Python3.7 using for example:
# remove the old symlink
rm -v $HOME/bin/python
# add the new one
ln -sv /Library/Frameworks/Python.framework/Versions/3.7/bin/python \
$HOME/bin/python
In all cases, you are responsible of having a good enough PATH (and preferably a short one).

Install a different version of Python

this might be a very simple question but I need your help. I work in a network and I cannot install the programs I want. Anyway, I need to use another version of python, which is installed in the directory /new_version/.
Now, when I type "python" in a shell (I use bash) the command point to the version of python installed in the machine I'm working with. I'd love that when I type "python" this command point to the /new_version/ which I've installed. It would be also better if I can call this "new version" with another command, i.e. python2.
I tried changing the PYTHONPATH in the .bashrc but it didn't work.
alias newpython="/path/to/your/new_version/python"
Add this to your .bashrc, you can then start the new python with newpython and the standard one with python.
Add the line
export PATH=/new_version/:$PATH
to your ~/.bashrc (or ~/.bash_profile) file. Then, whenever you run python, it will find the new version first in your PATH. Note this is PATH, not PYTHONPATH. See the comment by #Aaron.
Edit: Only do it this way if you want python to point to the new version. Use an alias as #cularis suggested if you want to call it something different, or make a symlink with:
ln -s /new_version/python /path/to/a/dir/you/add/to/your/path/newpython
Install virtualenv. With this you can easily set up different Python versions like that:
virtualenv -p /new_version/bin/python
Also, virtualenv enables you to easily install other Python packages via pip install.
And finally, there's a package called tox which can automate testing with different Python versions...

Categories

Resources