MacOS python command not finding active Conda environment from VS Code terminal - python

I recently got a new M1 MacBook - first time ever using a Mac - and immediately downloaded Miniconda to get it set up for some Python work.
I created some virtual environments (e.g. conda create -n myenv python=3.8) but when active, the python command defaults to Mac's Python 2.7, preventing me from running scripts from the command line in VS Code. For example:
conda activate myenv
(myenv)% which python
/usr/bin/python
where shows me the default 2.7 installation and the correct virtual environment version, but I can't access it.
(myenv)% where python
/usr/bin/python
/Users/user/miniconda3/envs/myenv/bin/python
Any idea how to get VS Code to find the proper Python version? It seems to work from the built in terminal, just not VS Code.
OS: Monterey 12.2.1
VS Code: 1.65.0
Miniconda: 4.10.1

Could you try to take advantage of python3 instead of python in the MacOS?
Or you can try to rename the python.exe to something others, such as python2.7 under the /usr/bin/python?

So the problem is, that in windows, you can change the path, but in mac there is this thing, that if you don't select the version, for example:
sudo python test.py
It will run Python 2.7, because it is installed, and it runs the lowest installed version. So try using
sudo python3 test.py
or specify the version in the terminal
sudo python3.8 test.py

Related

How to change python3 version on mac to 3.10.10

I am currently running python 3.9.13 on my mac. I wanted to update my version to 3.10.10
I tried running
brew install python
However it says that "python 3.10.10 is already installed"!
When i run
python3 --version
in the terminal it says that i am still on "python 3.9.13"
So my question is, how do i change the python version from 3.9.13 to 3.10.10? I already deleted python 3.9 from my applications and python 3.10 is the only one that is still there.
I also tried to install python 3.10.10 from the website and installing it. However it does not work. Python 3.10.10 is being installed successfully but the version is still the same when i check it.
Python 3.10.10 is already installed along with Python 3.9.13. Your path is probably pointing to 3.9.13 and that's why you're getting that Python version.
Try modifying your path variable to point to brew's Python installation, or (better yet) make use of a virtual environment.
a) Telling what Python version your PATH variable is pointing to:
$ which python
Will tell you what's the actual executable's path. That way, you are going to see exactly where you main python resides.
b) If you want to create a virtual environment with brew's python try something like:
$ /usr/local/Cellar/python#3.10/3.10.1/bin/python3 -m venv py310
$ source venv/bin/activate
$ python
> # you should be inside a 3.10.1 envornamente
> CTRL+D
$ pip install requests
you can use pyenv to work with multiple python environments
things to do:
install pyenv : brew install pyenv
install particular python: pyenv install 3.10.10
set python3.10.10 to gloabal python env: pyenv global 3.10.10
and can start using python 3.10.10 version
Just delete the current python installation on your device and download the version you want from the offical website. That is the easiest way and the most suitable one for a beginner.

How can I fix the "zsh: command not found: python" error? (macOS Monterey 12.3, Python 3.10, Atom IDE, and atom-python-run 0.9.7)

Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:
I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:
The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):
I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:
I deleted and then reinstalled the Python interpreter from python.org.
I tried alias python='python3' (which I saw in one of the threads here).
I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here).
To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again.
I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error.
Anyone updating their macOS to Monterey 12.3 will find that they suddenly no longer have the system-provided Python 2.
The reason for this is that Apple removed the system-provided Python 2 installation (details).
So a workaround/solution for this is to use pyenv to install Python 2.7 (or any other specific version you need).
Install pyenv with brew to manage different Python versions: brew install pyenv
List all installable versions with pyenv install --list
Install Python 2.7.18 with pyenv install 2.7.18
List installed versions with pyenv versions
Set global python version with pyenv global 2.7.18
Add eval "$(pyenv init --path)" to ~/.zprofile (or ~/.bash_profile or ~/.zshrc, whichever you need)
Relaunch the shell and check that Python works, or run $ source ~/.zprofile (Thanks masoud soroush!)
OK, after a couple of days trying, this is what has worked for me:
I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much).
I installed python via brew rather than from the official website.
It would still return command not found error.
I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3.
Problem solved.
As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.
If you simply installed Python 3, just use python3 as the command instead of just python. In my case, I had to install pynev first via Homebrew (executable brew) using brew install pyenv.
But still after using pynev to install Python 2.7.18 and setting it as a global version using pyenv global 2.7.18, I still ran into an error while trying to run python.
What worked for me (since I already had Python 3 installed) was by changing my command to use python3 instead of just python. Of course, this won't be a solution to everyone who may want to use Python 2.
you should try
sudo ln -s /usr/bin/python3 /usr/local/bin/python
that solve my problem:)
I just had this problem on a new Macbook Pro with macOS Monterey, and the below worked for me using Homebrew.
Using alias is not necessary when using Pyenv. Tested with Atom 1.60.0 and atom-python-run 0.9.7.
Install pyenv (https://github.com/pyenv/pyenv#installation) and its dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment):
brew install pyenv
brew install openssl readline sqlite3 xz zlib tcl-tk
Install Python 3.10.6 but I assume other 3.x versions should work as well:
pyenv install 3.10.6
Add Pyenv to your shell according to the instructions in https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv. In your home directory:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Make the installed Python available everywhere (this can be overridden per project or folder, if necessary):
pyenv global 3.10.6
Now, the output looks like this:
% which python
/Users/jl/.pyenv/shims/python
% python --version
Python 3.10.6
If some scripts still fail, check that you have added Pyenv to the necessary shell startup file(s) as mentioned in step 3 above.
Since you installed python3.9> (https://python.org/downloads/) , you just need to run everything with python3 instead of python. You don't have to downgrade your python to use it... It works fine with suffixing python with 3, i.e. python3 eg.
$ which python3, python3 --version, $ python3 script.py etc
I installed Python then this error occurred and I just run
echo "alias python=/usr/bin/python3" >> ~/.zshrc
and I t worked.on macOS Monterey 12.4, Python 3.8.9
Try the command python3 --version if you have installed Python 3.
I had the same problem with fresh MacOS Ventura with Apple Silicon M1 Pro chip. Seems like brew installs to a different location than before on Apple Silicon?
/usr/bin/ folder had a Python executable and the version for it was 3.9.6.
So I installed Python using brew but then I had to execute it via python3 command so I ran echo "alias python=/opt/homebrew/bin/python3" >> ~/.zshrc and restarted my terminal. I was prompted with the desired version of Python when executing python command.
alias python=/usr/bin/python3
This command works but need to re-enter whenever we restart the termian
If you are using pyenv and on Mac M1, then try switching the python from system version to a version that is installed already (ex: pyenv global 3.8.13, considering 3.8.13 is another version). In order to check what versions installed use pyenv versions.
If you don't have another version, try installing a new one and switch to that new environment (ex: pyenv install 3.8.13). Then install using pip. Everything should work fine. thats what worked for me.
and make sure your pip and python are aliased to pip3 and python3 respective in the respective terminal configuration file.
I First installed python with brew
brew install python
Then I tried linking the python with symlinks using:
brew link python3
And it worked for me.
PS: I was trying to run a shell script and alias approach didn't work.
I got zsh: Command not found python after installing using https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg
I solved it by configuring the ENVIRONMENT. I added a line, alias python=/usr/local/bin/python3.10, into the .zshrc file (you must be sure it is the true path).
My macOS (Apple silicon):
Darwin Johns-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64
for 3.10.8
install with python with brew command
brew install python#3.10
if you have .zshrc file just edit like this
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
if you dont have .zshrc file:
cd
vi .zshrc
and copy/paste the line below
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
I have a MacBook Pro with an Apple M1 chip and macOS v12.4 (Monterey).
I deleted all Python versions and cleared the trash
Opened Terminal and typed "python"
This prompted an xcode.app update
Then I downloaded the latest version of Python from python.org (version 2.10.5)
Installed that version
Quit Terminal
Opened Terminal
Typed "python3"
It seems to now be there OK.
I use brew install brew install python#3.9, after installation success I want to know the version of python, I run command python --version, and I got the problem zsh: command not found: python.
If you install it successfully, please check in the terminal. It will show you if you got problem in install if not problem you just change command from python --version to python3 --version.
Just run the below command in your terminal:
echo "alias python=/usr/bin/python3" >> ~/.zshrc
Basically, here we are saying the terminal to treat python as python3.
Works like magic!
eval "$(pyenv init --path)"
bash starting will be more slow, just suggest
export PATH=$PATH:$HOME/.pyenv/shims

Why there are two python versions in a virtual environment?

I have created a virtual environment using anaconda in VS. When the environment is active. I check the version using python --version, it gives the following output Python 3.9.9, whereas when I use which python and check the version from the path /usr/bin/python --version I get a different version Python 2.7.18. Why is that happening, and which version does the environment use?
Once your virtual environment is activated the python command will use the python version from your venv (located in path/to/.venv/bin/python).
which python and /usr/bin/python forces the use of the python version installed in /usr/bin/python which in your case seems to be version 2.7.18. If you want to change your default Python version (the one thats used with python without a venv being active) you can use sudo ln -s /usr/bin/python /usr/bin/python3.9.

How to get reticulate to recognize my miniconda python and my environments?

I am relatively new to python and would like to install it the "right" way (of which, of course, there are many) to use both natively and with R through reticulate.
Here's what I have done so far:
I reinstalled MacOS Catalina from scratch and installed Miniconda with homebrew.
Through miniconda, I have updated to python 3.8.5.
Then, I installed reticulate in R (R version 4.0.2., RStudio version 1.3.1056). When reticulate asked me whether I wanted to install miniconda, I declined (since it was already installed.
I aliased python3 with python in my zsh terminal.
This is where the trouble started. I couldn't (and can't) get reticulate to recognize my python installation or, if I can get reticulate to recognize it, I can't get it to recognize my python environment.
As advised elsewhere, I changed my RETICULATE_PYTHON to /usr/local/bin/python3, which actually matches one of my python installations. Now I get the error Error: could not find a Python environment for /usr/local/bin/python3
I also tried setting my path to my miniconda python, which is located somewhere in /usr/local/Caskroom/miniconda/base/. Or more specifically, as obtained by type -a python python3, in /usr/local/Caskroom/miniconda/base/bin/python3. Unfortunately, once I changed the reticulate directory to the miniconda version, type no longer returns /usr/local/Caskroom/miniconda/base/bin/python3, for reasons that I don't understand.
When I now run type -a python python3, I receive the following output:
python is an alias for python3
python is /usr/bin/python
python3 is /usr/local/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
python3 is /usr/local/bin/python3
I don't know what happened to my miniconda installation (which still seems to work fine, when I run conda --version, I receive conda 4.8.3).
So to summarize the status quo:
Running python from the terminal works (both interactive and to execute a py file)
Reticulate recognizes the path I set in RETICULATE_PYTHON
My miniconda no longer appears when I run type -a python python3
Most importantly: reticulate does not recognize my environment even though I have created an environment with miniconda in the terminal and another environment in Rstudio using reticulate::conda_create() and I have also made sure to activate them.
So my question is, how can I get reticulate to
Recognize my miniconda python
Recognize the various environments that I create with conda (both inside and outside of R)

How to install Spyder for Python 2 and Python 3 and get Python 3 in my Spyder env?

I have Python 2.7 installed (as default in Windows 7 64bit) and also have Python 3 installed in an environment (called Python3).
I would like to use Spyder as my IDE. I have installed Spyder3 in my Python3 environment, but when I open Spyder 3 (from within my Python 3 env), then it opens Spyder for python 2.7 and not python 3.5 as I would've hoped for.). I don't know why.
I have done TOOLS--Preferences--Python Interpreter -- Use the following Python interpreter: C:\Users\16082834\AppData\Local\Continuum\Anaconda2\envs\Python3\python.exe, but this didn't work either.
Many of us are running multiple python environments; I am sure some of you might have managed to use Spyder for these different environments.
Please tell me how I can get Python 3 using this method.
One possible way is to run activate Python3 and then run pip install Spyder.
So, when you create a new environment with: conda create --name python36 python=3.6 anaconda
This will create an env. called python36 and the package to be installed is anaconda (which basically contains everything you'll need for python).
Be sure that your new env. actually is running the ecorrect python version by doing the following:
activate python environmentwith: active python36
then type: python
this will indicate what python version is running in your env. It turns out, for some reason, my environment was running python2.7 and not 3.6
The cool thing is that anaconda distribution comes with spyder. Just be sure that you run Spyder from within your environment.
So to do this: activate python36
then type: spyder
It will automatically open spyder3 for python3.
My initial issue was therefore that even though i created a python3 environment, it was still running python2.7. But after removing the old python3 environment and creating a new python3 env. and installing the desired libraries/packages it now works perfect.
I have a 2.7 and 3.6 environment which can both be edited with spyder2 and spyder3 IDE

Categories

Resources