Homebrew python taking precedence over venv python - python-3.x

I have a few questions related to Python paths. I used homebrew to install python3 on my Mac:
# homebrew python
> which -a python
python: aliased to /usr/local/bin/python3.9
# system python
❯ which -a python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
/usr/local/bin/python3
/usr/bin/python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
I then set up a virtual environment using the command python -m venv venv. After activating it, I get:
> which -a python
python: aliased to /usr/local/bin/python3.9
/Users/kshitijsachan/Documents/venv/bin/python
> which -a python3
/Users/kshitijsachan/Documents/venv/bin/python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
/usr/local/bin/python3
/usr/bin/python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
My questions are:
Why does homebrew python take precedence over the venv python when I run which -a python with the venv activated?
Why is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 listed twice under which -a python3?
Is this the canonical setup for venv's with Homebrew python?

Related

venv create an extra link from python to python3

I'm using python3 v3.10.2 and after creating a virtual environment with :
/usr/bin/python3 -m venv myvenv
it create the following folder content :
activate
activate.csh
activate.fish
Activate.ps1
pip
pip3
pip3.10
python -> python3
python3 -> /usr/bin/python3
python3.10 -> python3
The fact that python points to python3 and override the default python(2) is an issue! (similarly for pip). I couldn't find and option to avoid this behaviour? Did I missed something?

My pip isn't working, for both versions of python

so i have installed both python 2.7 and python 3.8 however in the cmd if i type one of these:
pip install
pip --version
it uses the python 2.7 version. I don't think that i can use the packages installed with python 3.
i have tried:
Reinstalling python (2 & 3)
Adding it to my PATH
Changing the directory that it is saved in
Pip3 commands
bootstraping pip
yet none have worked.(also im on windows 7, and require the pip and python for both 2.7 & 3.8)
when i use:
python3 -m pip install x
or
py -3 -m pip install x
it comes up with:
DLL load failed while importing pyexpat: the specified module could
not be found
Python 3 command in Windows is python3. So if you want to run pip with python 3 you have to do python3 -m pip install.
You can also replace Python 2 path in your Path variable to your Python 3 path, like so when you'll run python command it will use Python 3 directly.
Hope i got useful.
Have a look at the official document,
https://docs.python.org/3/installing/index.html?highlight=pip#work-with-multiple-versions-of-python-installed-in-parallel
if you are on windows you can use,
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
hope this helps!
If you install both versions of python then by simply entering pip command will run that python pip whose path comes first. You could use pip3 for pip corresponding to python3 and pip2 for pip corresponding to python2. But the recommended way is to do following:
For Python 3
py -3 -m pip install
For Python 2
py -2 -m pip install

python venv creates a not-empty venv

I don't know much about python virtual environments so any help would be appreciated.
In my .bash_profile file there is an alias:
alias python='python3'
So I always use "python" command instead of "python3" command.
But, in terminal when I run:
$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
I don't know why are they different, and when I create a virtual environment with:
$ python -m venv myvenv
It doesn't create a venv with just "pip" and "setuptools" packages, otherwise it does OK when a run with:
$ python3 -m venv myvenv
Has anybody know what happend?
Many thanks!

Virtualenvwrapper does not initialize after updating Python to 3.6

My OS (Arch Linux) recently updated Python from 3.5.2 to 3.6.0, now when running any terminal I get this message:
/usr/bin/python: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Moreover I get these outputs
$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python2.7-config /usr/bin/python /usr/bin/python2.7 /usr/lib/python3.6 /usr/lib/python3.5 /usr/lib/python2.7 /usr/include/python3.6m /usr/include/python2.7 /usr/share/man/man1/python.1.gz
$ which python
/usr/bin/python
$ python --version
Python 3.6.0
How can I fix this situation? Will this situation will have any impact on my created virtual envs or other programs?
Thanks in advance.
This article was very useful to solve the situation, only you need is to run:
$ sudo pip3.6 install -U pip
$ sudo pip3.6 install -U virtualenvwrapper

Making 2.7.3 default version of python on CentOS 6

I want to execute python from php and I do have a script that works fine for default python interpreter. I have centos with default python 2.6.6 which is installed at /usr/bin/python and python 2.7.3 which is installed at /usr/local/bin/python2.7. You can see what is the default python version:
[root#me ~]# python -V
Python 2.6.6
[root#me ~]# python2.7 -V
Python 2.7.3
How do I make python 2.7.3 default python on my OS. So when i run python -V i should get 2.7.3?
I know it is bad. The alternative is to uninstall python 2.7.3 and I do not know how to do this.
Execute the below commands to make yum work as well as python2.7
echo "export PATH="/usr/local/bin:$PATH"" >> /etc/profile
source /etc/profile
mv /usr/bin/python /usr/bin/python.bak
update-alternatives --install /usr/bin/python python /usr/bin/python2.6 1
update-alternatives --install /usr/bin/python python /usr/local/bin/python2.7 2
update-alternatives --config python
sed -i "s/python/python2.6/g" /usr/bin/yum

Resources