how to get venv to point to older python version - python

I am trying to clone and run a project on my mac.
but on the installation options I have to run:
pip install -r requirements.txt
I get an error and I am pretty sure its because the project runs on python 2.7 while my venv runs on 3.4
(venv)/*
$ python --version
Python 3.4.3
however when I am outside of venv I run am back onto the correct python version:
$ python --version
Python 2.7.6
WHy is venv running 3.4? How can I change venv to not run 3.4 and just run the 2.7
Do I have to update an environment or venv variable on my system? I can't find where python is in my system. Also any advice on what commands to run would be appreciated as I am a bit new to python/django.
perhaps the pip I have in venv is wrong, idk. Any help is appreciated.

You can use the option -p to specify, which version should be used, when you create the virtual environment.
For example:
virtualenv -p python2.7 venv

Related

How to change Python version in VS Code

so I want to make a project using python and have followed docs to install it but when I trying using it on vs code my version of python is 2 and not 3 even tho i've select my interpreter as python 3. I've check and python 3 is installed in my computer.
1) This is most probably because your environment variable paths include python 2. Add python3 environment variable to your paths if you haven done so.
2) In terminal, u can set your cmd line code to run with $python3 helloworld.py
3) https://code.visualstudio.com/docs/python/environments
You should be using virtual environments whenever you do something with Python (exception can be when writing single file scripts with no non-standard dependencies, and specifying the Python version with the shebang). That way, you'll completely isolate your environment.
The problem you're having now is that you rely on the path to your python. What happens if someone needs to run your project on his machine? And what he needs to install some dependency, but there's some conflict with the ones already install? Virtual environments help you with that isolating your environment and making it portable.
Here's a quick start, which will help you to understand it:
❯❯❯ which python
/usr/bin/python
❯❯❯ python --version
Python 3.8.3
❯❯❯ python3.7 -m venv venv/
❯❯❯ source venv/bin/activate
venv ❯❯❯ which python
/tmp/python/venv/bin/python
venv ❯❯❯ python --version
Python 3.7.7
# Once you're done, you can deactivate the environment
venv ❯❯❯ deactivate
❯❯❯ which python
/usr/bin/python
❯❯❯ python --version
Python 3.8.3
This way, I've created a Python3.7 environment that is completely isolated from my system.
See offical docs.
EDIT: I've just seen that the title was changed to a VS specific question. You can still do it with the command line (and it's worth knowing how to do it and how it works), but this explains how to manage the virtual environments within the editor.

Cannot activate correct Python in remote computer

I have moved a virtual environment onto a remote computer. I am trying to activate python 3.8 but not having much luck.
The virtual environment I'm using was created using venv. But I created it on my Mac and then moved it onto the target Linux computer. The following demonstrates that the Linux computer has python 3.8
kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate_this.py pip pip3.8 python3
activate.csh easy_install pip2 python python-config
activate.fish easy_install-2.7 pip2.7 python2 wheel
Activate.ps1 easy_install-3.8 pip3 python2.7
I activated the virtual environment with the following commands:
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
However, the following command shows that python 2.7 was activated
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python --version
Python 2.7.13
Further, when I ran one of my programs I got a syntax error that only python 2.7 would throw:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3 fix_mistakes.py
File "fix_mistakes.py", line 113
p = print
p (f"{round(c - b,0)} seconds")
SyntaxError: invalid syntax
Even when I run the command python3, it activated python 3.5 as demonstrated by the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
I even think that the computer is not even using my working environment but the default python interpreter due to the following:
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/code$ which python
/usr/bin/python
I would think that the intepreter would be in my virtual environment which would be:
/venv_byu/bin/python3
#####UPDATE
I was able to install venv on the linux but I'm still activating python 3.5.3
kylefoley#kfoley76:~/byu_corpus_small$ source venv_byu/bin/activate
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ which python
/home/kylefoley/byu_corpus_small/venv_byu/bin/python
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small$ python --version
Python 3.5.3
Apparently the environment I downloaded did not have 3.8
(venv_byu) kylefoley#kfoley76:~/byu_corpus_small/venv_byu/bin$ ls
activate activate.fish easy_install-3.5 pip3 python
activate.csh easy_install pip pip3.5 python3
Now, I just need to figure out how to get 3.8
Virtual environments are not portable. You should create a new virtual environment on the destination computer and populate it with the packages you need. Listing them with pip freeze on your local computer or manually enumerating them in requirements.txt are two common approaches.
The standard virtual environment shipped with Python actually hard-codes the path of the virtual environment, so you can't even rename the directory locally, much less copy it to a different directory structure on a different computer.
The activate command needs to be run with source for various reasons, but that also means that it is not very robust against failures. For example, it can throw an error or fail silently, but still update your prompt so that it appears as if the virtual environment was successfully activated.
To create a new virtual environment on a computer where you have Python 3.8 installed as /usr/local/bin/python3.8, you can run
/usr/local/bin/python3.8 -m venv venv_byu
You then need to activate this environment, and pip install or otherwise populate it with the libraries you need.
pip freeze will list the exact versions of all installed packages, so it is more precise in getting exactly the same version of everything. If you manually list packages in requirements.txt, you don't have to specify a precise version of anything, and you can leave out packages which are pulled in as dependencies of the packages you actually specifically depend on.

python3 -m venv: how to specify Python point release/version?

To create a virtual environment using virtualenv you can specify the Python release and point version like so:
virtualenv --python=python3.6 .venv
How can I achieve this using Python3's venv module (as in python3 -m venv .newvenv)? According to the documentation using venv is the recommended way to create virtual environments but I didn't see how I can choose a virtual environement with a specific Python version.
Run venv with whatever Python installation you want to use for the new virtual environment. For example, if you would run your Python 3.6 installation with python3.6, then
python3.6 -m venv whatever
would be how you create a Python 3.6 virtual environment.
I thought to add to this answer when one is using pyenv. In my workflow I use pyenv to have multiple python versions but not to manage virtualenvs. I rather have my python virtual environment in the project's root. With pyenv one can install multiple python versions by running pyenv install 3.8.10 and after that pyenv install 3.9.0. When you run pyenv versions you should get something similar to this
system
* 3.8.10 (set by /Users/<user>/.pyenv/version)
3.8.10/envs/python-test.venv
3.9.0
When working on a project and choosing what python version should be used in that project you can do the following.
$ mkdir my_project && cd my_project
$ pyenv global <version>
$ python --version // should be the version you set as global
$ python -m venv .venv
$ source .venv/bin/activate
I was able to avoid error mentioned in the comments by using the option --without-pip. Then after activating the venv, I installed pip manually with the get-pip.py script.

How to use a Python virtual env in my Ubuntu bash?

I am trying to package my project with a virtual env so that it is easier to implement.
I am trying to do this in a Ubuntu bash.
I have succesfully created a Python venv using the Python virtualenv library.
I do manage to activate it using source venv_name/bin/activate.
I can indeed see (venv_name) at the beginning of my command line.
However, I do not manage to actually use this virtual environment.
I have for proof that when I type which python3 I get my root python3; and I have tried to update a package in the virtualenv but it has been updated in the root python.
What should I do to actually use my virtual env ? For now I am trying:
python3 myscript.py
And it is working but I suspect it's running with my root python3.
I think you have two versions of python (2 and 3). You create virtualenv with python 2. Recreate virtualenv with correct python version
You have to make next steps to use python3 virtual environment in Ubutnu:
1. Install virtual environment lib for python3 with command:
pip3 install virtualenv
2. Create your virtual environment:
python3 -m venv venv
3. Activate it:
source venv/bin/activate
Works correct for me in Ubutnu 16.04

Virtualenv is installing with wrong version of Python

I recently have started learning python and have run into an issue.
When I run python on my mac without virtualenv, the version number is Python 2.7.5. Unfortunately, when I go into my virtualenv, and run Python, the version number is Python 2.6.1.
I tried, creating another virtualenv using:
virtualenv -p /usr/bin/python2.7 newdev
but got: The executable /usr/bin/python2.7 (from --python=/usr/bin/python2.7) does not exist
You have to specify the executable, not the whole path. For example:
mkvirtualenv --python=python3 mynewenv
virtualenv --python=python3 mynewenv

Categories