python venv creates a not-empty venv - python-3.x

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!

Related

Homebrew python taking precedence over venv python

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?

venv activate has no effect

I want to use a virtualenv for my python3 project:
did virtualenv -p python3 venv to install it.
when doing . venv/bin/activateit doesn't activate the venv (I'm able to use all my lib's in standard path) also no (venv) before prompt. There is also no error, it just gives me a new line.
source venv/bin/activate gives me the same result.
Interestingly my VSCode detects when virtualenv -p python3 venv is run and asks if I want to change the environment.

How to change python env

I have a issue where if a python 3 script's shebang line is /usr/bin/env python3 then script will be interpreted with /usr/bin/local/python3
type -P python3
/usr/local/bin/python3
So PATH python3 is /usr/local/bin/python3 but this creates problems where some packages are not available for /usr/local/bin/python3 and I would instead like to use /usr/bin/python3 as default env python3.
Using a alias to set python3 to /usr/bin/python3 can be done but this does not solve the issue. Using Virtualenv if fine but one can not create a virtualenv for every single litte script out there.
type -a shows that there are two python3 defined on my system so there must be a way to change the prefered one:
type -a python3
python3 is aliased to `/usr/bin/python3'
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
How can I change so that env python 3 is /usr/bin/python3?
Best regards
I solved the issue by editing /etc/environment. The change that I did was to specify to read /usr/bin before /usr/local/bin as was configured on my computer. After that I restarted my PC and now /usr/bin/python3 is the default env python.

Creating a virtualenv using Python 3 error

I have python in ConEMu loaded and am trying to creating a virtual environment to work in.
I used
pip3 install virtualenv --user
and it said that virtualenv was usccessfully installed, however then I try to
virtualenv .venv
and i get the error message
bash: virtualenv: command not found
wondering if anyone else has run into this problem
virtualenv is built into python 3, and called venv. You don't need to install anything with pip.
You can make a virtualenv in python 3 like this:
python3 -m venv ve

How to get virtualenv to run Python 3 instead of Python 2.7?

On Mac, if I simply open a new terminal window and run:
python --version
I get:
3.6
but if I do this:
virtualenv venv && source venv/bin/activate
and then, in that environment, I run :
python --version
I get:
2.7
I need virtualenv to run 3.6. How do I do that?
This :
/usr/bin/python
is 2.7 but this:
/usr/local/bin/python
is 3.6. The path for my normal user has /usr/local/bin come up before /usr/bin/. Is virtualenv running as someone else? How do I control its path?
I ran this:
virtualenv -p /usr/local//Cellar/python/3.6.5/bin/python3 venv
but then I do this:
virtualenv venv && source venv/bin/activate
and I'm running in an environment with 2.7.
On Python 3 you don't need virtualenv script anymore, you should just use the venv module included with standard lib:
python3 -m venv myvenv
But if you really want to keep using the old virtualenv script, you can - specify the interpreter explicitly with the -p option:
virtualenv -p /path/to/python3 myvenv
The easiest way is to change python globally to Python3 as I think you're using it more often than Python 2.7 (or hopefully always). To achieve this, add the following line of code at the end of your .bash_profile:
alias python='python3'
virtuanenv is using /usr/bin/python, hence it should work now.
If you don't want to change it globally, than you should use the following command to create your Python3.6 virtual environment:
python3 -m venv venv
or the explicit Python version if you have multiple Python3 versions installed:
python3.6 -m venv venv
On more suggestion at the end: I recommend you to read something about pipenv as it's the new recommended way to handle virtual environments and your whole package management at once. It's super easy and fixes a lot of common issues. Here's a nice article from realpython.com on that topic.
Hope I could help you. Have a nice day.

Resources