pyenv unable to find docker-compose - python

I think I may have screwed up my local py environment on my Mac.
I've installed pyenv and a couple of python versions:
$ pyenv global
2.7.17
3.7.7
It seems to have decided to wrap itself around docker-compose...
$ which docker-compose
/Users/wintermute/.pyenv/shims/docker-compose
I've got a local env I want to run python3 in:
$ cd mysite
$ pyenv version
3.7.7 (set by /Users/wintermute/mysite/.python-version)
But now I can't run docker-compose in it:
$ docker-compose up
pyenv: docker-compose: command not found
The `docker-compose' command exists in these Python versions:
2.7.5
Note: See 'pyenv help global' for tips on allowing both
python2 and python3 to be found.
And worse, it won't even run in the version it says to use:
$ pyenv local 2.7.5
$ docker-compose up -d
ERROR:root:code for hash md5 was not found.
(and a load of debug traceback messages)
I've tried pyenv rehash. I think I originally had python installed via homebrew. What can I do to unscrew my local environment? Why can't it find docker-compose? What have I done??

I was using the command eval "$(pyenv init -)" in my ~/.zshrc to enable pyenv. You need to check your .bashrc or .zshrc for a similar command. This modifies the PATH and put the shims first which creates the issue. (You can run pyenv init - to see what happens). Remove the eval and manually added the shims to the path AFTER the existing PATH.
export PATH="${PATH}:"$(pyenv root)/shims
I haven't thoroughly tested it, but it fixes the docker-compose issue.
Check out Pyenv Advanced Configuration for more info.

Related

How can I fix my pyenv setup to install packages correctly?

The issue:
So, as per recommendation I have uninstalled local python versions (except for the Mac default one) and installed pyenv-virtualvenv through brew. (see: [installed python packages in wrong folder][1])
However, whenever I try to install something through pip/pip3/python/python3 it will install the package in the folder /opt/homebrew/lib/python3.10/site-packages although there is no python 3.10 installed in /opt/homebrew (or cellar) anymore. I also ran brew doctor and brew cleanup --prune=all, both seemingly fine.
Summary:
Since the packages are installed to a wrong path, I cannot use them, as it will state the module is not found.
My Setup:
Specs:
Macbook Air M1 Ventura
Visual Studio Code Version: 1.75.0
check for versions:
myusername#MacBook ~ % which pip
/Users/myusername/.pyenv/shims/pip
myusername#MacBook ~ % which pip3
/Users/myusername/.pyenv/shims/pip3
myusername#MacBook ~ % which python
/Users/myusername/.pyenv/shims/python
myusername#MacBook ~ % which python3
/Users/myusername/.pyenv/shims/python3
myusername#MacBook ~ % pyenv versions
system
* 3.11.1 (set by /Users/myusername/.pyenv/version)
3.11.1/envs/venv_mypyenv_playground
venv_mypyenv_playground
myusername#MacBook ~ % which brew
/opt/homebrew/bin/brew
check for PATH$:
myusername#MacBook ~ % echo $PATH
/Users/myusername/.pyenv/shims:/Users/myusername/.pyenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/Library/Apple/usr/bin
While there's the path /Users/myusername/.pyenv/shims, there is no path: /Users/myusername/.pyenv/bin, but: /Users/myusername/.pyenv/versions/3.11.1/bin
Maybe that is part of the problem? If so, how can I amend the PATH$ correctly?
Check on Bash and ZSH Setup:
I was wondering if there's something wrong with some bash or zshcr files, which causes python to prioritize homebrew. This is how they look:
My setup:
.bash_profile
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
.bashrc is empty
.zprofile
# set PATH, MANPATH, etc., for Homebrew.
eval "$(/opt/homebrew/bin/brew shellenv)"
.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
Do I maybe need to change export PATH="$PYENV_ROOT/bin:$PATH" this, so the none-existent path /Users/myusername/.pyenv/bin, will be corrected as: /Users/myusername/.pyenv/versions/3.11.1/bin? How would it look like correctly then? export PATH="$PYENV_ROOT/versions/3.11.1/bin:$PATH"?
Answer: I tried this, but it didn't change anything. Packages are still loaded to homebrew library.
I am really at a loss, at what's causing the issue, as the used python versions seem to be ok and belonging to pyenv.
Would it be also an option to uninstall brew and try again? or would that break my current pyenv setup?
Might my bash profile help to figure things out?
% nano .bash_profile
#MacBook~ % source .bash_profile
‘FROM BASH_PROFILE’
HOME=/Users/myusername
HOMEBREW_CELLAR=/opt/homebrew/Cellar
HOMEBREW_PREFIX=/opt/homebrew
HOMEBREW_REPOSITORY=/opt/homebrew
INFOPATH=/opt/homebrew/share/info:
LC_CTYPE=UTF-8
LOGNAME=myusername
MANPATH=/opt/homebrew/share/man::
OLDPWD=/Users/myusername
PATH=/Users/myusername/.pyenv/shims:/Users/myusername/.pyenv/versions/3.11.1/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/Library/Apple/usr/bin
PWD=/Users/myusername
PYENV_ROOT=/Users/myusername/.pyenv
PYENV_SHELL=zsh
SHELL=/bin/zsh
(some things. I am going to skip:)
pyenv:12: command not found: pyenv
Thank you!
[1]: Installed python packages will stay in site packages 3.10?

Why is my pyenv showing the wrong active python version?

I'm using an Intel MacBook Pro and have problems with python and pyenv.
Can anybody explain how this can happen and how I can correct it?
[~] pyenv versions
system
* 3.10.1 (set by /Users/....../.pyenv/version)
[~] python -V
Python 2.7.16
[~] pyenv --version
pyenv 2.2.3
[~]
In my .zshrc file I have
# set correct python version using pyenv
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
In my opinion python -V should return 3.10.1 and not 2.7.16 which seems to be the system version.
I installed pyenv using brew and run MacOS BigSur 11.6.2.
I was able to follow Tim Roberts' suggestion to create a symlink in order to solve this problem on my own Mac. I am writing this answer to help clear up any ambiguity you might have.
The commands I used were:
cd /usr/local/bin
ln -s /usr/local/bin/python3 python
python -V
Which gives me:
[16:57:25] Richards-MBP:bin richardkyu$ python -V
Python 3.8.12
Typing in python to the command line opens up the 3.8.12 interpreter. Also, my python3 is installed by brew I believe.
Following up on the comment, I should clarify that I created a new symlink for python rather than rewriting the one for python3. When I execute
ls -l grep python3
in the /usr/local/bin directory, I get the following output, which I assume is similar to yours:
I am not sure of the exact issue with these symlinks you're seeing, but if you can clarify what you're worried about I would be glad to check it out.

How to downgrade python version from 3.8 to 3.7 (mac)

I'm using Python & okta-aws tools and in order to fetch correct credentials on aws I need to run okta-aws init. But got an error message of Could not read roles from Okta and the system prompted that"Your Pipfile requires python_version 3.7, but you are using 3.8.3 (/usr/local/Cellar/o/1.1.4/l/.venv/bin/python).
I've tried to search all the Pipfiles on the mac and it seems that the Pipflie under my ~/Pipfile and /usr/local/Cellar/python#3.8/3.8.3_2/libexec/bin/Pipfile all have the same python version of 3.8, while the Pipfile under my /usr/local/Cellar/okta-aws-tools/1.1.4/libexec/Pipfile has required python_version = 3.7.
I've been struggling with this for a while and really not sure how I can fix this.
Consider installing pyenv with Homebrew on macOS
brew update
brew install pyenv
OR Clone the repository to get the latest version of pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Define your environment variables
(For a recent MacOS you may want to replace ~/.bash_profile with ~/.zshrc as that is the default shell)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
Restart your shell so the path changes take effect
exec "$SHELL"
Verify the installation and check the available python versions
pyenv install --list
Install the required python version
pyenv install 3.7
Set it as your global version after installation
pyenv global 3.7
eval pyenv path
eval "$(pyenv init --path)"
Verify your current python version the system is using
python3 --version
I recommend you to install and use pyenv, a Python Version Management.
Once intalled pyenv, install python 3.7:
pyenv install 3.7
And then set the environment PYENV_VERSION to version of python you want to use, on this case will be 3.7:
pyenv shell 3.7
brew only approach.
rm -rf $(brew --repository)/Library/Taps/company
brew tap-new company/team
brew extract python#3.7 company/team --version=3.7.9
HOMEBREW_NO_AUTO_UPDATE=1 brew install company/team/python#3.7.9
brew link --force company/team/python#3.7.9
This creates a local tap, extracts python 3.7.X to a formula in that local tap and then installs and links that formula
The created local tap and the new formula file can be found in
$(brew --repository)/Library/Taps/company/homebrew-team
Reinstalled xcode-select (used solution here)
Downgraded python 3.9 to 3.8.12 (used #Shayan's solution here)
Restarted terminal and checked default Python version, that's all
This is my solution for M1 Mac

Why can't I find ansible when I install it using setup.py?

Because I had some trouble with Ansible (I'm on mac) which seemed to be fixed in the latest dev version today I uninstalled ansible through pip (sudo pip uninstall ansible) and reinstalled the latest dev version from the github repo using the classic setup.py method, which seemed to end successfully (full output here.
So then I tried using it:
$ ansible --version
-bash: ansible: command not found
$ which ansible
$
I checked where it is installed. From the full output I linked to above I found that it is installed in /usr/local/lib/python2.7/site-packages, and indeed in there I find an egg:
$ ls -l /usr/local/lib/python2.7/site-packages | grep ansible
drwxr-xr-x 4 root admin 136 Aug 22 16:33 ansible-2.4.0-py2.7.egg
When I start Python and check the site-packages folder I find a different folder:
>>> import site; print site.getsitepackages()[0]
/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
but that is a symlink to the same folder:
$ ls -l /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
lrwxr-xr-x 1 hielke admin 54 Aug 13 22:36 /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages -> ../../../../../../../../../lib/python2.7/site-packages
So I guess the problem is that no symlink is created to the ansible package in /usr/local/bin/. But I'm unsure how I could create such a symlink and why it wouldn't appear in the first place.
Does anybody know how I can move forward from here? All tips are welcome!
When you invoke ansible from the shell, bash will search in your $PATH for a file named ansible that is executable. This may not be the only issue, but this is the immediate cause for the error you're seeing. The .egg file itself is not an executable, it's just a file used for distributing the code.
If ansible has been installed correctly, you should be able to find it by using locate or the OSX Finder GUI. The name should match exactly, with no file extensions. You will probably also find ansible-connection, ansible-console, etc. in the same place where you find the ansible executable. If you find it, great! Test it out and add that directory to your $PATH in a terminal like so:
export PATH=$PATH:/path/to/ansible
Where /path/to/ansible is the directory where you found the executables. This change to the $PATH variable is temporary, and will go away when you close your shell. If you can now run ansible from bash, then you can make the change permanent by adding that export to the end of your $HOME/.bash_profile file, or by adding a rule in /etc/paths.d (recommended by Apple). See more on how exactly to do those here if you are unfamiliar with them.
Now, if that's not the problem and you can't find the ansible executable, then the installation itself is your problem. You might also try using a virtual environment (if you have it installed) to make sure that the version you're pulling from github isn't broken:
git clone https://github.com/ansible/ansible.git
cd ansible
virtualenv venv
source venv/bin/activate
pip install .
which ansible
As of this writing, the above gives me a working ansible install.
For those using the Windows 10 Ubuntu terminal, running this command should fix the issue:
export PATH=$PATH:~/.local/bin
Find where ansible reside on your Mac. Most times its /Users/<yourusername>/Library/Python/3.7/bin
or /Users/<yourusername>/Library/Python/2.7/bin. Then ...
export PATH=$PATH:/Users/<yourusername>/Library/Python/3.7/bin
You can store this in your .bashrc file.
Well, I think you just need to create a soft link
ln -s /Users/${yourname}/Library/Python/${python version}/bin/ansible /usr/local/bin/ansible
pip3 install ansible --user
this installs in ~/.local. just include this in PATH, it will work
example: export PATH="$PATH:~/.local/bin"
I faced same issue when I installed ansible. Run the below commands to solve the issue. But we have to active each time when we open a bash session if we want to use ansible.
$ python -m virtualenv ansible
$ source ansible/bin/activate
$ pip install ansible
I'm using zsh so in my /Users/arojas/.zshrc I add this line that's where my Ansible got installed by Python
export PATH="$PATH:$HOME/Library/Python/3.7/bin"
I faced the same issue and after have installed it used pip3 install ansible it all works now.
I suggest uninstalling Ansible and re-installing it using pip according to the method suggested in the Ansible docs:
Or if you are looking for the latest development version:
pip install git+https://github.com/ansible/ansible.git#devel
If you are installing on OS X Mavericks, you may encounter some noise from your compiler. A workaround is to do the following:
$ sudo CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install ansible
Readers that use virtualenv can also install Ansible under virtualenv, though we’d recommend to not worry about it and just install Ansible globally. Do not use easy_install to install ansible directly.
system/instance used: ec2 RH8, as root
# pip3 install ansible (not recommended - should be by a user)
# ansible --version ( not found - wtf?!)
# yum install mlocate
# update
# locate ansible (long output; scroll to where you input command)
# export PATH=$PATH:/usr/local/bin
# ansible --version (success, Yes!)
system/instance used: ec2 RH8, as root
# pip3 install ansible (not recommended - should be by a user)
# ansible --version ( not found - :( )
# yum install mlocate
# updatedb (updatedb creates or updates a database used by locate)
# locate ansible (TL;DR)
# export PATH=$PATH:/usr/local/bin (Add this line to .bashrc)
# source .bashrc (To reflect the changes in the bash)
# ansible --version (success, Yes!)

Allow use of system python in conda env?

Is there a way to force conda to use the system version of python (along with all of the system libraries) in a given env?
I have conda enabled by default in my shell, which can get a bit annoying, because if I try to run a system python app, it gets a different version of python to what it is expecting (python still defaults to 2.7 on *buntu), and often won't run. I would like the root env of conda to just be a redirect to the system python install.
You need to edit all user shell run commands such as your .bashrc file to prepend the bin directory of anaconda to path export PATH=~/anaconda/bin:$PATH, while in your root run commands append export PATH=$PATH:~/anaconda/bin. In both cases you will have access to the conda command. You can check which python will be run by typing $env python --version. You can also check which other versions would be available and their order of priority (if the other is removed) by using $type -a python. Of course ensure your executable python files have #!/usr/bin/env python and not some other direct route to a python executable. For further info Google BASH Shell look up queries like http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html.
Simply removing the python symlink from ~/miniconda3/bin/ appears to do the job.
$ which python
/home/naught101/miniconda3/bin/python
$ rm /home/naught101/miniconda3/bin/python
$ which python
/usr/bin/python
$ source activate science
discarding /home/naught101/miniconda3/bin from PATH
prepending /home/naught101/miniconda3/envs/science/bin to PATH
(science)$ which python
/home/naught101/miniconda3/envs/science/bin/python
(science)$ source deactivate
discarding /home/naught101/miniconda3/envs/science/bin from PATH
$ which python
/usr/bin/python
So far, this doesn't seem to have caused me any problems. Unfortunately the same doesn't work for ~/miniconda/bin/python3, because conda requires it when switching to other envs that use the same python version. However, that one hasn't caused as many problems in the first place.
If this does cause problems, it's easy enough to undo, just cd ~/miniconda/bin/; ln -s python3 python (or what ever version of python you're using in your conda root env). You may need to activate/deactivate an env to get that version of python back on your PATH.
If you are in (base) or another environment, use conda deactivate, this will exit conda's environment and place you back into the OS environment:
on MacOS
(base) user$ python --version
Python 3.9.12
(base) user$ conda deactivate
user$ python --version
Python 2.7.16
on Linux (with no base python installed)
(base) user$ python --version
Python 3.9.15
(base) user$ conda deactivate
user$ python --version
-bash: python: command not found

Categories