How to change Python version in VS Code - python

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.

Related

Python versions are not changing despite activating virtual environment in WSL2

Background:
In WSL2 (ubuntu 20.04) I created a python virtual environment inside a directory. Using the command python3 -m venv venv my system's python version was set to python3.11 (after downloading) via sudo update-alternatives --config python3 and then choosing the version. I noticed I was having some errors of missing modules when I started WSL2 (happening after a computer restart), I read this was because I was using a different python version than the one ubuntu 20.04 came with so I switched back to 3.8 via the config menu as before. I am also using VS code that's connected to my WSL2.
These are some of the contents of my venv directory: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3
Question:
After activating my virutal env via source venv/bin/activate, when I do python3 --version I still get a version of 3.8.10 despite creating the virtual environment with 3.11. I was able to get the interpretor set to 3.11 on VS code.I know I was in the virtual environment since my command prompt had (venv) in front. I went into the python console while in the virtual env and did import sys and sys.path this was my output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload']. Why isn't the python version changing, am I misunderstanding something or did I not do something correctly? Seems like pip isn't working either but works when I switch my system python to 3.11 (I tried installing it on 3.8 but it said it was already installed).
Solved:
Answered below, just re-created the virtual env while making sure my system python version was 3.11 (may have been some mixup earlier).
By changing the selection in sudo update-alternatives --config python3 you change the selected python version also for the chosen vitrual environment (at least when using venv, it might be different with other tools).
That can cause issues, because when creating a new virtual environment envname using venv from a specific python version xx.xx, a directory named pythonxx.xx is created in /envname/lib/, and inside it a directory named site-packages that contains the packages installed by the pip of this specific environment.
So changing back to the original python version of the environment through sudo update-alternatives --config python3 should solve the issue, and probably the errors of missing modules are due to the incompatibility of the current selected python version with the original version which you installed the virtual environment from.
Personally, to avoid confusing, I name my virtual environments with the python version as a suffix, e.g envname_py3.11.1. But there might be a better method which I am not aware of.
I deleted my venv directory and recreated my virtual environment while on python3.11. This has resolved my issue.

Rename python 3.7 executable without breaking stuff

I have renamed python.exe to python37.exe to avoid conflict with other versions. It works for running python, but if I run pip37.exe (located in /Scripts) I get the following error:
Fatal error in launcher: Unable to create process using '"c:\python37-32\python.exe" "C:\Python37-32\Scripts\pip37.exe"
Is there a way to keep python.exe renamed to python37.exe, but keep all python tools working?
This sounds like A BAD IDEA.
There are tools designed to help you manage exactly this sort of thing. The best of which imho is pyenv: https://github.com/pyenv/pyenv
It's quite simple to install. It takes a bit of getting used to – wrapping your head around virtual environments – but it makes it all so much easier to work with ultimately.
E.g. On my system I have the following versions of python:
pyenv versions
system
2.7.10
* 3.5.6 (set by /Users/.pyenv/version)
3.5.6/envs/core4
3.6.4
3.6.4/envs/core5
core4
core5
The one with the asterisk is currently the global version, which will be the one used from any default shell. I can change that using pyenv global 3.6.4 for example. I can also create virtual environments. E.g. core4 and core5 are virtual environments I created for specific projects. Each of these will have their own different libraries installed by pip install and differing python versions. You can activate a virtualenv for a given shell session e.g. pyenv activate core5.
And if you're thinking "what on earth does this have to do with Windows", look here: https://duckduckgo.com/?q=Windows+Subsystem+for+Linux&atb=v93-1__&ia=web and here: http://timmyreilly.azurewebsites.net/python-pip-virtualenv-installation-on-windows/
On Windows Python installs PyLauncher. You don't need virtual environments or renaming tricks. py.exe is in the standard Windows path, has command line switches to pick the Python version to use, and enables using "shebangs" to specify what version of Python to run for scripts:
py script.py # Run the latest Python installed (or specified by PY_PYTHON environment variable).
py -2 script.py # Run the latest Python 2 version installed.
py -3 script.py # Run the latest Python 3 version installed.
py -2.7 script.py # Run the specific Python version.
py -2.7-32 script.py # Run the 32-bit specific Python version.
py -0 # List Python versions installed.
Scripts can use shebangs similar to Linux:
#!python2
#!python3
#!python2.7
#!python2.7-32
To run pip with a specific version:
py -2.7 -m pip install ...
If you need still would like a virtual environment with a specific Python version, you can specify the version (e.g. -3) and use:
py -3 -m venv <my_env_name> # to create an virtual environment
<my_env_name>/scripts/activate # to activate that environment
Activation adds the virtual environment to the path, so python (not py) will run in that environment. the Scripts directory in that environment will also be added to the path, so pip can be run directly as well to install packages in that environment.

Safely have two versions of Python on Cloudera virtual machine without Python installation

Presently we have a big-data cluster built using Cloudera-Virtual machines. By default the Python version on the VM is 2.7.
For one of my programs I need Python 3.6. My team is very skeptical about 2 installations and afraid of breaking existing cluster/VM. I was planning to follow this article and install 2 versions https://www.digitalocean.com/community/tutorials/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4
Is there a way "I can package Python 3.6" version in my project, and set the Python home path to my project folder, so that there is no installation that needs to be done on the existing Virtual machine?
Since we have to download python and build source for the Unix version, I want to skip this part on VM, and instead ship the folder which has Python 3.6
It seems that miniconda is what you need.
using it you can manage multiple python environments with different versions of python.
to install miniconda3 just run:
# this will download & install miniconda3 on your home dir
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh -b -p ~/miniconda3
then, create new python3.6 env:
conda create -y -n myproject 'python>3.6'
now, enter the new python3.6 env
source activate myproject
python3
miniconda can also install python packages, including pip packages and compiled packages. you can also copy envs from one machine to another. I encourage you to take a deeper look into it.
ShmulikA's suggestion is pretty good.
Here I'd like to add another one - I use Python 2.7.x, but for few prototypes, I had to go with Python 3.x. For this I used the pyenv utility.
Once installed, all you have to do is:
pyenv install 3.x.x
Can list all the available Python variants:
pyenv versions
To use the specific version, while at the project root, execute the following:
pyenv local 3.x.x
It'll create a file .python-version at the project root, having the version as it's content:
[nahmed#localhost ~]$ cat some-project/.python-version
3.5.2
Example:
[nahmed#localhost ~]$ pyenv versions
* system (set by /home/nahmed/.pyenv/version)
3.5.2
3.5.2/envs/venv_scrapy
venv_scrapy
[nahmed#localhost ~]$ pyenv local 3.5.2
[nahmed#localhost ~]$ pyenv versions
system
* 3.5.2 (set by /home/nahmed/.python-version)
3.5.2/envs/venv_scrapy
venv_scrapy
I found it very simple to use.
Here's a post regarding the installation and basic usage (blog post by me).
For the part:
Since we have to download python and build source for the Unix
version, I want to skip this part on VM, and instead ship the folder
which has Python 3.6
You might look into ways to embed Python interpreter with your Python application:
And for both Windows and Linux, there's bbfreeze or also pyinstaller
from - SOAnswer.

dyld: Library not loaded: #executable_path/../.Python

A while ago I had both Python 2.7 and 3.5 installed on my Mac and was able to use them both successfully. Not too long ago, I installed Anaconda and IPython. I have used those for a couple weeks for prototyping and in-console programming.
After I went back to the regular Python for my Django and Flask projects, I discovered an unpleasant thing. Namely, whenever I try to run python or python3 I get the following error:
dyld: Library not loaded: #executable_path/../.Python
Referenced from: /Users/name/anaconda3/bin/python3
Reason: image not found
Abort trap: 6
When I run conda I also get the same error.
If I create a new virtual environment with virtualenv django-project, I am able to activate it, and it allows me to run Python 2.7 successfully.
My question is the following: How can I fix python and python3 for the command line while also retaining the working Anaconda and IPython? How can I make sure that the virtual environments are able to carry Python 3?
I also use macOS and I never mess with or even deal with the system python. I've installed python3 via Homebrew (https://brew.sh) and I always use a virtual environment. I have one in my home directory (my default) and I have one for each project I'm working on.
Your rule of thumb should be to never run 'pip' if you aren't within a virtualenv. Check with $ echo $VIRTUAL_ENV.
To create/recreate the virtual environment in python3 with the currently installed libraries:
Get into your project directory and active your virtualenv.
(optional) Dump your requirements via pip: $ pip freeze > requirements.txt
Nuke your virtual environment directory if you have one: $ rm -rf .venv
Deactivate it: $ deactivate
Create a new one with python3: $ virtualenv -p python3 .venv
Activate it: $ source .venv/bin/activate
(optional) Install your requirements: $ pip install -r requirements.txt
Profit.
You can skip the steps for writing and reading the requirements.txt if you just want to create a new virtual environment and then install only the modules you want/need later.
firstly, to reduce your confusion n which python you are using you could try the following 2 commands in Linux or mac where bash shell is installed:
$ which python
or
$ which python3
in my case, it outputs the python paths I am using with pyenv [with fish shell] [$ is a shell sign]

Changing Python version on OSX

I have python 2.7 installed on mac and I would like to use the CRYPTO module, but it doesn't support so installed home-brew and then downloaded python 2.7.9. I changed the path on ~/.bash_profile to /usr/local/bin
Yet, when I try which python it uses the default one.
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
I suggest you use virtualenv to create a virtual enviroment for your project, which provides you isolated Python environment specific to one Python interpreter.
Official Python package installation guide.
Quick example:
virtualenv -p /path/to/my/python venv
source venv/bin/activate
pip install pycrypto
If you need to activate virtualenv in your .bash_profile add line (. instead of source):
. /Users/you/venv/bin/activate

Categories