asked on
Python not available in Conda environment after reinstall MiniForge on Macbook Pro
Hi,
I'm running VSCode with Python on my Mac.
Now I had issues with MiniForge so I uninstalled it & installed it again.
Now when I create a conda environment and activate it, it doesn't recogniz python.
If I install it via conda install python it is there again, but that is not the correct way, right?
I a default (not Conda) environment, the command python is there.
Thanks for your input.
J
ASKER
Python is not recognized but Python3 IS recognized, please advise howto continue.
ASKER
So suddenlty Mac doesn't recognize the Python nore the pip install commands anymore, but it does with Python3 and Pip3, should I now go and change all my scripts and use Python3 and pip3? Then when Python4 comes I will need to change again to Python4?
ASKER
Ended up creating symbolic link so python3 points to python
sudo ln -s /usr/local/bin/python3 /usr/local/bin/python
ASKER
However, still then conda environment doesn't accept python as command :-(
ASKER
Now this is the output of my brew installs of python, thanks for your input!!!
==> python@3.11: stable 3.11.5 (bottled)
Interpreted, interactive, object-oriented programming language
Not installed
From: python@3.11.rb">https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/python@3.11.rb
License: Python-2.0
==> Dependencies
Build: pkg-config
Required: mpdecimal, openssl@3, sqlite, xz
==> Caveats
Python has been installed as
/opt/homebrew/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/python@3.11/libexec/bin
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/opt/homebrew/lib/python3.11/site-packages
tkinter is no longer included with this formula, but it is available separately:
brew install python-tk@3.11
gdbm (`dbm.gnu`) is no longer included in this formula, but it is available separately:
brew install python-gdbm@3.11
`dbm.ndbm` changed database backends in Homebrew Python 3.11.
If you need to read a database from a previous Homebrew Python created via `dbm.ndbm`,
you'll need to read your database using the older version of Homebrew Python and convert to another format.
`dbm` still defaults to `dbm.gnu` when it is installed.
For more information about Homebrew and Python, see: https://docs.brew.sh/Homebrew-and-Python
==> Analytics
install: 250,509 (30 days), 687,658 (90 days), 1,590,581 (365 days)
install-on-request: 140,906 (30 days), 382,821 (90 days), 797,812 (365 days)
build-error: 311 (30 days)
ASKER
I'm calling on my python files via python in my scripts, thanks for helping me out!
ASKER
Ended up installing Python 2, seems to work, but not sure if I'm doing the right things. Thanks for your input. Would like to run some sanitizing checks on my system to know I didn't mess up.
#!/bin/bash
# Download Python 2.7.18 installer
echo "Downloading Python 2.7.18 installer..."
curl -o python-2.7.18-macosx10.9.pkg "https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg"
# Install Python 2.7.18
echo "Installing Python 2.7.18..."
sudo installer -pkg python-2.7.18-macosx10.9.pkg -target /
# Verify installation
echo "Verifying installation..."
python2 --version
# Cleanup
echo "Cleaning up..."
rm python-2.7.18-macosx10.9.pkg
echo "Python 2.7.18 installation complete."
ASKER
Extra info: I now get message about older Python which I didn't had before. I do think things are messed up.
$ pip install ...
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
ASKER
Note: the conda environment doesn't also sees packages installed like interpreter, which is working fine not in Conda. Tried full removal of the Conda and reinstall too (via the MiniForge install using Brew, did remove the conda directory manually). If I then try to install it within the Conda environment (the interpreter), it says:
ollecting open-interpreter
ERROR: Could not find a version that satisfies the requirement open-interpreter (from versions: none)
ERROR: No matching distribution found for open-interpreter
ASKER
What about this approach? Let me know what you think then I can execute it.
https://gist.github.com/MuhsinFatih/ee0154199803babb449b5bb98d3475f7
Ok, You're going about it wrong. You should be setting environment variables, not messing with new installations and uninstallations.
What do you get when you type?
which python
I get
/opt/anaconda3/bin/python
What about when you type that within conda?
Then, try typing python followed by tab to force a command completion. What choices do you get? Try it both in your shell and then in conda and see what differences you have.
When I type python followed by tab, I have multiple versions of python, but Anaconda picks the correct one.
python python3 python3.10 python3.11 python3.8 python3.9 pythonw
python.app python3-config python3.10-config python3.11-config python3.8-config python3.9-config
You likely need to set up the correct environment variable to point to the correct python. Here's a sample of environment variables for Anaconda:
CONDA_PREFIX=/opt/anaconda3
CONDA_PROMPT_MODIFIER='(base) '
CONDA_PYTHON_EXE=/opt/anaconda3/bin/python
CONDA_SHLVL=1
Miniforge is based off Anaconda, so it should use the same variables and should have set these when you installed it. If not, you can manually set them.
You also need the correct path. At the begining of mine, I have
PATH='/opt/anaconda3/bin:/opt/anaconda3/condabin:
DO NOT USE THIS PATH AS IS. I chopped off the extras after the colon. You will break your path if you just copy that line. Those 2 were added to the beginning of the rest of the PATH.
I purposely bought the last intel model before the M1 came out because I knew there would be problems with M1 during the first few years, just as there was when they switched to Intel from PPC and when they switched from MacOS to OS X.
ASKER
Thanks! But it kind of started to work, but now run into package conflicts.
So I would like to start from clean slate.
What do you think about this script based upon the link I shared, any comments/concerns running it?
#!/bin/bash
set -e # Exit on any error
# Function to log messages with timestamps
log() {
local msg=$1
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "$timestamp: $msg" | tee -a process.log
}
# Confirm user's intent to proceed
log "Beginning process to clean up Python environment and reinstall Python..."
read -p "Are you sure you want to proceed? This will modify your system setup. (y/N): " choice
choice=$(echo $choice | tr '[:upper:]' '[:lower:]')
if [[ "$choice" != "y" ]]; then
log "User canceled the process."
exit 0
fi
# Check for Homebrew and install if not found
if ! which brew > /dev/null; then
log "Homebrew not found. Installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
log "Homebrew detected."
fi
# Check for the shell being used
shell=$(echo $SHELL)
log "Detected shell: $shell"
# Uninstall Miniforge or Anaconda if detected
if which conda > /dev/null; then
log "Miniforge/Anaconda detected. Proceeding to uninstall..."
conda install anaconda-clean
anaconda-clean --yes
sed -i.bak '/conda.sh/d' ~/.zshrc ~/.bashrc # Clean shells for conda
else
log "Miniforge/Anaconda not detected."
fi
# Uninstall all Python packages
log "Uninstalling all Python packages..."
pip uninstall -y -r <(pip freeze)
# Remove pip cache
log "Removing pip cache..."
rm -rf ~/Library/Caches/pip
# Uninstall pip
log "Uninstalling pip..."
pip uninstall -y pip
# Remove third-party Python installations (backup first)
log "Removing third-party Python installations..."
[ -d "/Library/Frameworks/Python.framework" ] && sudo mv /Library/Frameworks/Python.framework /Library/Frameworks/Python.framework.bak
# Remove Python paths from shell rc files (backup first)
log "Removing Python path entries..."
cp ~/.zshrc ~/.zshrc.bak
cp ~/.bashrc ~/.bashrc.bak
sed -i.bak '/Python.framework/d' ~/.zshrc ~/.bashrc
# Update shell rc file for Miniforge
log "Updating shell rc file to include Miniforge..."
if [[ "$shell" == "/bin/zsh" || "$shell" == "/usr/bin/zsh" ]]; then
echo 'source ~/miniforge3/bin/activate' >> ~/.zshrc
echo 'conda activate' >> ~/.zshrc
elif [[ "$shell" == "/bin/bash" || "$shell" == "/usr/bin/bash" ]]; then
echo 'source ~/miniforge3/bin/activate' >> ~/.bashrc
echo 'conda activate' >> ~/.bashrc
fi
# Install Python3 using Homebrew
log "Installing Python3 using Homebrew..."
brew install python
# Download and install Miniforge
log "Downloading and installing Miniforge..."
curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-x86_64.sh
bash Miniforge3-MacOSX-x86_64.sh -b
rm Miniforge3-MacOSX-x86_64.sh
# Initialize Miniforge
log "Initializing Miniforge..."
source ~/miniforge3/bin/activate
conda init $(basename $shell)
# Verify installation
log "Verifying installation..."
conda --version
# Final log message
log "Process completed. Your Python environment should now be clean and freshly installed."
ASKER
Note what I found on a forum:
Conda Support's Recommendation
- Conda support advises using the conda init to set up a conda "shell function" keeping other stuff off the PATH.
- The activation ensures that Anaconda's PATH is upfront but in a less permanent manner.
- Activation also handles additional requirements like setting additional environment variables required by some packages.
What You Should Do Instead of Messing with PATH
- Use conda init to automatically configure your shell to use Anaconda.
- Follow the prompts during installation to let Anaconda modify your shell's initialization script. This ensures proper setup without altering the PATH.
- Run source ~/.bashrc (Linux, macOS) or source ~/.bash_profile (macOS) to refresh your current shell session for immediate changes.
This approach ensures that Anaconda's commands are available without risking any conflicts or potential issues that come with manual PATH modification.
ASKER
Damn, I got lots to learn. Another reason to start over, never did this: "just because you’re in a conda environment doesn’t mean that pip belongs to that environment. Instead you must run conda install pip for pip to be associated with that environment. Then every pip install will be tied to that environment."
ASKER
Troubleshooting all the time now.
The script wasn’t a success. It wants to update Conda to install Conda-clean, so I abandonded that road for now.
So I looked at what you mentioned:
$ which python
/opt/homebrew/Caskroom/miniforge/base/bin/python
(base)
$ conda deactivate
$ which python
/usr/local/bin/python
$ pip install open-interpreter
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
ERROR: Could not find a version that satisfies the requirement open-interpreter (from versions: none)
ERROR: No matching distribution found for open-interpreter
$ pip3 install open-interpreter works fine
ASKER
Another update: running a script via shift + enter opens a Juniper Notebook in VSCode, then my code works fine.
However, running it in in a Conda environment gives error which do not make sense/which I don't have in the Juniper console and which I never had before this issue.
File "main.py", line 31
log_message(f"Starting the script.", log_file)
Really looking for a solution here, maybe a reinstall of the Mac?
ASKER
Finally made it work, dont know what step actually triggered that.
Would like to check though with some scripts current health.
You're probably missing an environment variable that points conda to the correct python.
Here's some instructions:
https://towardsdatascience.com/from-soup-to-nuts-guide-for-setting-up-a-conda-environment-58afc7c4801?gi=4baf49bc26f5
You should probably scroll down to the Environment section, which is about halfway down the page.