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

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)

Related

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

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

Inplace of python executable inside virtual environment, getting Alias

I was running an old python version of python 3(3.5.2), and because of that, some of the libraries(like the seaborn) were misbehaving. I tried upgrading python to 3.9 with the help of brew and was successfully able to do that. I can now see python inside the /usr/local/bin/python3.9. I also upgraded the pip and virtualenv package. After that, I unlink /usr/local/bin/python and then linked it with /usr/local/bin/python3.9. Next, I set alias in ~/.bash_profile to /usr/local/bin/python.
After performing the above process, python -V gives the new python(ie python 3.9).
But, when I now create the virtualenv with the command -
virtualenv nvenv --python=/usr/local/bin/python
the virtual environment is created but inside nven/bin/ I could see an alias of the python3.9 and not the executable(like I had in my old virtual environment).
I tried with the following commands too but the result was same -
virtualenv nvenv
python -m virtualenv nvenv
I have performed so many steps without really understanding much and now I am confused now where I am going wrong. Could anyone help me to create the virtualenv with my latest python3.9 which also has python3.9 executable inside bin of it? I need it to set interpreter in the spyder IDE.

Pipenv not recognizing shims for PyEnv python installation

I'm attempting to use pipenv and pyenv to create a virtual environment using python 3.9.0 for a project, but am not able to get pipenv to recognize the version of python 3.9.0 installed by pyenv. Oddly, using pyenv python 3.8.6 works just as expected, and I'm specifying the specific python I want pipenv to use, similar to this answer.
I'm running Catalina 10.15.7 and had a coworker reproduce this exact same behavior on his machine.
Both Pipenv and pyenv have been installed via Homebrew. I have since tried uninstalling homebrew Pipenv and reinstalling with pip, with the same results.
To Reproduce
I successfully ran pyenv install 3.9.0, and when I run pyenv versions, I see:
system
3.8.6
3.9.0
Just as I'd expect.
Inside my working directory, I run pyenv local 3.9.0 to switch my local version of python, followed by pyenv version, which returns returns:
3.9.0 (set by /Users/my-name/myproject)
indicating that the local version was set.
To double check, running pyenv which python returns:
/Users/my-name/.pyenv/versions/3.9.0/bin/python
Now, if I run pipenv install --python 3.9.0 I get the following:
This already is suspect, since 3.9.0 is my local version already. If I respond with y, I get:
Which seems odd, as when I run python, I get to python 3.9.0, so clearly it is on my path. A Pipfile is not created for me, and I don't get a pipenv virtual environment.
Now, if instead I run pipenv install --python $PIPENV_PYTHON, which should in theory use the version of python on my path (which, again, should be 3.9.0), I get this:
Notice how it says "using /users/..../python (3.9.0) to create virtualenv", which is correct, however the next line down says "created virtual environment CPYTHON3.8.6...".
Now, if I repeat the above steps using python 3.8.6, everything works fine.
Additionally, if I specify pipenv --python ~/.pyenv/versions/3.9.0/bin/python3.9, that also seems to work fine -- so for some reason it's not respecting the version, but it is respecting the binary.
The same is true if I run pipenv install --python $(pyenv which python), since it's effectively the same thing as the above command.
For reference my environment vars:
PIP_PYTHON_PATH=/usr/local/Cellar/pipenv/2020.11.15/libexec/bin/python <- perhaps this is a problem?
PIPENV_ACTIVE=1
PIPENV_PYTHON=/Users/myname/.pyenv/shims/python
PYENV_ROOT=/Users/myname/.pyenv
And my $PATH starts with PATH=/Users/my-name/.pyenv/shims:
I do happen to have python 3.8 and python 3.9 installed via homebrew as well, however I'm not trying to use these, and since my path leads off with the shims, I feel like this should not be a problem. Running which python3 and which python both point to the "shims" folder.
Summary
Pipenv is not respecting my pyenv shims, despite the fact that my PYENV_ROOT and my PATH are set correctly. Running pipenv install --python $(pyenv which python) works, specifying a python version does not.
Is this a problem with pipenv, or pyenv, or something that I'm doing?
Per the pipenv docs:
Pipenv by default uses the Python it is installed against to create
the virtualenv. You can set the --python option to $(pyenv which python) to use your current pyenv interpreter.
It was probably using your system python versions rather than your shims

Python not recognised in windows prompt cmd

Anconda prompt:
where conda
C:\Users\shail\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\shail\AppData\Local\Continuum\anaconda3\Scripts\conda.exe
C:\Users\shail\AppData\Local\Continuum\anaconda3\condabin\conda.bat
where python
C:\Users\shail\AppData\Local\Continuum\anaconda3\python.exe
C:\Users\shail\AppData\Local\Microsoft\WindowsApps\python.exe
So i went to environment variables and added path of anaconda and python. Path added:
C:\Users\shail\AppData\Local\Continuum\anaconda3
C:\Users\shail\AppData\Local\Microsoft\WindowsApps
C:\Users\shail\AppData\Local\Continuum\anaconda3\Library\bin
C:\Users\shail\AppData\Local\Continuum\anaconda3\Scripts
C:\Users\shail\AppData\Local\Continuum\anaconda3\condabin
But when I type on windows cmd:
C:\Users\shail>conda --version
conda 4.8.3
C:\Users\shail>python --version
C:\Users\shail>
So there is no response from windows cmd on version of python, so I guess it is not recognising python. I mainly need to use pip command, for that I need python first. I dont have python separately installed on system. It is inside my anaconda. On some sites it is written I should not download python separately and add its path because that will confuse the computer since there will be 2 pythons; one from Anaconda and one downloaded separately. So sticking to the anaconda version of python.
How to get my anaconda version of python recognised ?
pip worked for me after restarting my computer. Though python is still not returning any output but pip is working fine.

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