How to change python env - python-3.x

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.

Related

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!

How to use direnv with python venv?

I have this line in my .envrc
source python3-venv/bin/activate
I basically created a python3 venv inside the folder where the .envrc is located.
However even through the .evnrc is executed (e.g. all now env variables are set correctly when I enter this directoy), the venv is not activated.
When I do which python3, it shows /usr/local/bin/python3 instead of <path to my current folder>/python-venv/bin/python3
If I run the source line manually, the bash prompt will switch to (python3-venv) my-workstation:working-folder antkong$ as expected.
How can I get the python-venv correctly activated?
I am running it in MacOS Mojave.
I removed the venv then rebuild the venv
python3 -m venv python3-venv
It seems to fix the issue.

Create Virtual Environment using Python Documentation

I am very new at command line usage. I am using python 3.7.2, Bash and VSCode Integrated Terminal. I am trying to create a virtual environment using venv and following python documentation:
https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
The command to use is this one:
$ python3 -m venv test-env
and I get:
bash: python3: command not found
Later I have found a similar answer in an stackoverflow post:
How to create and activate virtual environment in windows 10 using bash command
And I use the command:
py -m virtualenv test-env
and I get this:
No module named virtualenv
I am very new using the command line so i don´t really know what is going on and how to work it around.
Hi i can see that you are using two different tools to create your environment.
Those are "venv" and "virtualenv".
Venv is a library that already comes with your python installation.
Virtualenv is an external one.
I had the same problem before and the solution is very simple.
I recommend you to stick with venv because it works pretty ok and you don´t need to do extra job installing external libraries.
So for solving your problem the Bash Shell is telling you that the command Python3 has not been found.
So try instead just:
python -m venv test-env
Sometimes Python documentation is not accurate enough and I know when you start using commands, accuracy in the sintax is extremely important.
Try this steps,it'll helped you:
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env (OR, python -m venv env)
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....

No module named virtualenvwrapper?

I'm trying to install openCV on my new Mac (Mojave).So, I'm following this tutorial.
I want to create a new virtual whit this command:
mkvirtualenv cv3 -p python3
But sometimes I get the following output on my terminal:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/keyone/code/.virtualenvs/cv3/bin/python3.7
Not overwriting existing python script /Users/keyone/code/.virtualenvs/cv3/bin/python (you must use /Users/keyone/code/.virtualenvs/cv3/bin/python3.7)
Installing setuptools, pip, wheel...
done.
/usr/bin/python: No module named virtualenvwrapper
or this output:
-bash: mkvirtualenv: command not found
This is how my .bash_profile file looks like:
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:$PATH
export WORKON_HOME=$HOME/code/.virtualenvs
export PROJECT_HOME=$HOME/code
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
I looked on stack overflow to find a solution, but nothing is working for me. virtualen, python etc. are already installed.
Try using virtualenv -p python3 envname as it work for me in mac book : D

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