Changing default idle (&pip) in a python3 virtual env - python

I have created a virtual environment for a python3 project.
phil#shuttle:$ python3 -m venv venv
phil#shuttle:$ source venv/bin/activate
(venv) phil#shuttle:$ python -V
Python 3.5.3
(venv) phil#shuttle:$
However, when I am in it, idle still defaults to python 2.7. The idle3 command work fine.
Can I change the default version of idle within the virtual environment (and not outside of it) so that don't keep using the wrong version?
(Supplementary question: do I have to do similar for pip/pip3?)
[Running Ubuntu 17.04]

IDLE is run by python, not the reverse. If you run
(venv) phil#shuttle:$ python -m idlelib
where python is 3.5.3, then that python will start the IDLE that comes with 3.5.3.
I do not have idle or idle3 commands on Windows, so I cannot answer questions about it.
To run pip inside the venv, replace idlelib with pip and add pip arguments. If pip is not installed inside the venv, first run python -m ensurepip and then run pip to upgrade pip.

Related

Python and pip installation on Mac don't show version?

On Mac OS.
Downloaded the Python from their website.
Python -V return Python 2.7.16, and
python3 -V return Python 3.9.4
Installed pip with : python3 get-pip.py, got Successfully installed pip-21.0.1
But when I run pip -V
I get File "/usr/local/bin/pip", line 1.... SyntaxError: invalid syntax
After reading here a lot, i could not understand (in simple words for dumbs) :
How could you "alias" or update python to show/run in version 3+ ?
Why I can't get the pip version if it's installed ?
Use pip as a module instead
% python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Anyone who's interested , what i had to do is to
CD the project on terminal
run python3 -m venv env (this create virtual environment of python3 inside project)
run source env/bin/activate activate it.
now pip --version or python --version will return the right results.
From my basic knowledge i understand that the mac is using python2 which we don't want to interrupt, so we install python3 and create a 'virtual environment' for it inside our project folder, once its in, we can install all other things.
to deactivate (stop the virtual env) we run deactivate

Make the 'python' phrase refer to Python 3 on Linux?

So whenever I run python -v it says I'm using Python 2, why is python reserved for Python 2 on Ubuntu, and is there a way to make python reserve python 3? The latest version.
For example, Id prefer to write
`python /files/app.py`
To execute my files than write
`python3 /files/app.py`
It seems messy to have to add the 3, you don't on Windows, so why is Ubuntu different?
You can use an alias to do this. Add the following line to your .bashrc:
alias python=python3
Don't forget to reopen your terminal, or do source ~/.bashrc for the changes to take effect.
Duplicate of this post.
Ubuntu does this because you probably have two versions of Python installed, a system variant and a 3.x variant you are using. You can try python --version and python3 --version to see. If they return different things, you have two installations.
For example:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
To circumvent the alias use the command built-in command:
$ command python --version
Python 2.7.6
Another way to circumvent the alias is to use \ before the command.
$ \python --version
Python 2.7.6
To disable the alias in the current shell use the unalias built-in command:
$ unalias python
$ python --version
Python 2.7.6
As already stated here, you have system python and user python and one option is to create aliases for python and pip on a user level ~/.bashrc
Another option is to create a virtual environment for each project you work on. The advantage of this is that you only install modules relevant to each project.
Create a virtual environment
mkdir someproject
cd someproject/
python3 -m venv venv
Activate environment
source venv/bin/activate
Check python and pip version
$ pip -V
pip 9.0.1 from ~/someproject/venv/lib/python3.6/site-packages (python 3.6)
$ python -V
Python 3.6.7
Deactivate environment
deactivate
Python, pip and any other modules you decide to install is installed in the venv folder instead of system wide.
Only gotcha is to remember to activate your environment.

Virtualenv doesn't use right version of Python

I'm working in Amazon's Cloud9.
ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
ec2-user:~/environment/flask_init $ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ec2-user/environment/flask_init/venv/bin/python3
Also creating executable in /home/ec2-user/environment/flask_init/venv/bin/python
Installing setuptools, pip, wheel...done.
ec2-user:~/environment/flask_init $ source venv/bin/activate
(venv) ec2-user:~/environment/flask_init $ python -V
Python 2.7.14
Why is the virtual environment not using Python 3?
Please note that this question is not a duplicate of this one. The issue was specifically to do with the way the Cloud 9 environment sets up Python alias.
I tried your flow on my machine and everything works as expected.
dluzak#Karol-PC:/tmp$ python -V
Python 2.7.12
dluzak#Karol-PC:/tmp$ virtualenv -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
dluzak#Karol-PC:/tmp$ source venv/bin/activate
(venv) dluzak#Karol-PC:/tmp$ python -V
Python 3.5.2
(venv) dluzak#Karol-PC:/tmp$
Nonetheless I personally use virtualenv as module when creating venv with python 3: python3 -m virtualenv venv. Maybe this would work.
You provided very little details. Have you installed virtualenv for both Python 2 and 3? Are you sure Python 3 interpreter works fine?
Edit:
After investigation in comments we found out that the problem was in bash settings configured by Amazon. It seams that Amazon configures bash (probably in ~/.bashrc) to replace python calls with an alias. To fix this a call unalias python before enabling venv is needed. It is described in Amazon docs
When I was using virtualenv earlier today, I had the same problem that my env was not using the right version of python.
Instead of activating my environment like this:
source activate
I found that activating it like this actually worked:
source ./activate
Hope this is helpful!
Here is how i create virtualenv on Cloud9
Python 3.4
$ sudo pip install virtualenv
$ virtualenv -p /usr/bin/python3.4 venv
$ source venv/bin/activate
Python 3.6
$ sudo apt update
$ sudo apt install python3.6-venv
$ python3.6 -mvenv venv
$ source venv/bin/activate
I have encountered a similar issue.
In my case did not work because I moved the virtual env folder (but the same thing happens when you rename it).
You can understand which version of python (and thus which module will import) is using by typing
$ which python
If it write something like:
/usr/bin/python
Then it means your virtual env is not being activated.
To solve this issue, instead of creating a new virtual environment, you can simply edit the script activation file in your env:
$ nano venv/bin/activate
And edit the following line with your absolute path of your virtual environment:
VIRTUAL_ENV="/YOUR_ABSOLUT/PATH_TO/venv"
Hope it helps
:)

How can I make venv install a python3.6 binary?

When I run python -m venv, the virtual environment directory that venv creates includes a binary named python and another named python3 which is just a link to python. (In my installation, python is Python 3.6 and python2 is Python 2.7.)
My problem is, sometimes (and I can't understand what's the difference between subsequent invocations) it also creates another symlink python3.6 pointing to python, but sometimes it doesn't. I need this symlink (actually, tox needs it). The binaries pip3.6 and easy_install-3.6 are always installed in the virtualenv.
Is there any way I can make sure that python -m venv creates a symlink python3.6?
(Disclaimer: I'm using pyenv to manage my Python installation, but I can reproduce the behavior above using /usr/bin/python -m venv)
When creating venvs (python -m venv, not virtualenv), I've had success by including the version number in the call to create;
python3.6 -m venv myvenv
You need to have the binary already available to get venv to use it, but if you have it, it shouldn't matter if you use python2 -m venv or python3 -m venv. If you want 3.6, try:
python -m venv python=`which python3.6` ~/envs/py36

About virtualenv and version of python?

I need help in understanding with venv. I have installed venv with virtualenv venv -p python3.6.
I have activated it (venv) and install django
pip django`install django`
And so, when I work with my project should I always activate venv or not? Because I run my manage.py without venv and using python2, but I need python3.
And then I run with active venv with python3 I got mistakes like this:
ModuleNotFoundError: No module named 'forms'
I also use python3.5.2 and I created a virtual environment using the following command
python3 -m venv venv
And activated it using the following command
. venv/bin/activate
I always activate the virtual environment before running the application
You must activate the virtualenv before calling pip install ... (potentially using pip3 with Python 3.x) and also every time you need to work with the virtualenv (e.g. before calling python manage.py ...)

Categories

Resources