I launched JupyterLab which is using a Python 3 kernel. However, when I am running the following commands below, it is showing different Python versions.
!python -V
Output: Python 2.7.18
from platform import python_version
print(python_version())
Output: '3.6.10'
I am expecting the Python 3.6.10 version to appear as it is the kernel that I launched/that is running. Is there a way to activate the Python 3 version whenever I run the "!python -V" command?
The original question is about python kernels so I quickly answer that.
Kernels are independent of python that you run you jupyter with.
Try this command:
jupyter kernelspec list
You will see your kernels. If you want to have another one here is the doc (https://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments)
not going deep into that as I can see this is probably not the actual problem.
It seems to me you want to change the interpretor called by "python" command.
You did not specify your OS. So in short:
In MacOs update the symbolic link (How to set Python's default version to 3.x on OS X?)
In Ubuntu use update alternatives (Unable to set default python version to python3 in ubuntu)
In Windows - hmm, haven't use windows in a while so probably here (https://superuser.com/questions/1576758/how-do-i-alias-python3-on-windows)
Related
I just downloaded VS Code this week and have run into a problem accessing different versions of Python.
When I run a python file called set.py from the command line via python set.py, VS Code is accessing the Python 2.7 version that came with my MacBook. When I run python --version it confirms that VS Code is using Python 2.7.16.
However, when I right-click in the editor and select Run Python File In Terminal it appears to be using the updated version 3.9.4. I have tested this by using an fstring (available in 3.9 but not in 2.7). The fstring code throws an error in the first method but clears fine in the second. Here's my code:
s = set()
s.add(1)
s.add(2)
s.add(3)
print(f"The set has {len(s)} elements.")
And here's my terminal with the results of both methods shown.
Does anyone know what exactly is going on, and how I can execute python files via the command line?
TIA
Specs:
I am running VS Code 1.55.2 and have downloaded Python 3.9.4 to my MacBook Pro running Big Sur 11.2.3
I have also downloaded the Python extension in VS Code and have selected Python 3.9.4 64-bit as my interpreter.
On macOS and linux, you will have a system python that tends to be old and you don't want to touch it because there are system utilities that rely on it.
You will also have the user-installed python, which here is python 3.9.
If the system python is python2, then the command python will typically invoke python2 and the command python3 will invoke python3.
In VS Code on Windows, when you launch a terminal window from the IDE, it will activate the interpreter/environment you've chosen for your project (typically by CTRL-SHIFT-P and then Python: Select Interpreter). I've noticed on Linux that it will not do this (and this may also be the case on macOS), so if I want to use a specific version or environment, I need to specify it or activate it as my first command in the terminal with something like conda activate myenvname.
Unlike in the terminal, VS Code will use the selected interpreter if you run code directly from the IDE (using Run Without Debugging or Control + F5).
I am using Visual Code 1.47.0 on Windows 8.1 with the latest version of the Python extension installed too. I just installed a module using pip. I then created a new Python file to test it. The IDE highlights the import statement as an error and the drop-down info box says "Unable to import 'extpylib'. If I try to run/debug the test file, I get a traceback error saying:
C:\Users\User\AppData\Roaming\Python\Python38\Scripts
Exception has occurred: ModuleNotFoundError
No module named 'extpylib'
File "C:\Users\user\Documents\PythonProjects\test-extpylib.py", line 2, in <module>
import extpylib
If I run the pip install command again, even with the "--upgrade" command line argument, it says everything is installed and that I'm using the latest build. I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit? I am not using the Code Runner extension.
I assumeg that this mismatch of versions between the IDE and the VSCODE Terminal window is causing my issue? If so, how can I get the VSCODE Terminal window to use the same version as that which is selected in the IDE? If not, what is the real problem and how can I fix it?
You may have many python paths and VS code is using the wrong one. Choose the right interpreter path just clicking here:
I solved this by closing and re-opening the vs-code after installing new package.
It seems like an environment issue. In my own Visual Studio code integrated terminal, when I type which python and which pip, I get the correct path to both python and pip executables. So this:
I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit?
Definitely seems to be the issue.
If your VSCODE terminal still uses Python 2.7, you could follow the tips presented here:
However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.
and
Note: conda environments cannot be automatically activated in the integrated terminal if PowerShell is set as the integrated shell. See Integrated terminal - Configuration for how to change the shell.
and
Changing interpreters with the Python: Select Interpreter command doesn't affect terminal panels that are already open.
Of course, the package you installed in an environment is independent of other environments.
As you said the VSCode Terminal using Python2.7 while the status bar shows the selected interpreter is Python3.8.3 64-bit. This meaning you are using the global python(3.8.3) to run the python file while you install the package to the Python(2.7) environment.
Solution:
After you selected the python interpreter in the status bar, you need to activate the environment in Terminal through shortcut 'Ctrl+Shift+`'. You can through these commands to check which environment you are using:
In the terminal:
By command "pip --version" to check which pip you are using.
By command "python" -> "import sys; sys.executable" to check which python you are using.
By command "python" -> "import sys; sys.path" to check which 'site-package' the interpreter searching for.
pip command that was used to install the package is based on a different version of python versus what VS Code is using.
Check the python version of your pip command, run pip --version. For example, on my system, this is the output of this command.
$ pip --version
pip 22.0.4 from /Library/Python/3.8/site-packages/pip (python 3.8)
Check the version of code that VS code is using. For VS code 1.61, the python version will be visible at the bottom left corner while a python file is open in the editor. Screen shot of the same is given below. You can also observe from the screen shot that lazy_streams package is not being recognised by VS code.
To solve the error in VS code, update the python version in VScode to be same as that of pip. Updating of the version can be done by simply clicking on the Python version displayed, which will further prompt you to select among all available python versions on your system.
I just installed my ubuntu on my PC then i installed python 3 & Vscode on my PC. but my VS Code didn't recognize the python3. maybe my vs code looking for my python. how to solve this?
In my VS Code when I compiled the class then the output:
python -u "/home/kisiniki/Documents/python/tempCodeRunnerFile.py"
/bin/sh: 1: python: not found
In my terminal already installed python 3.6.8.
First, try to see if you are able to run the same command in terminal.
python -u "/home/kisiniki/Documents/python/tempCodeRunnerFile.py"
If you are getting the same error, "python: not found", then likely python installation was not completely successful, you can either uninstall and reinstall or you can try to update PATH to contain the folder containing the python executable. See https://askubuntu.com/questions/637848/how-to-reset-python-path-to-usr-bin-python for more detail.
If you can run the command in terminal, then that means VS Code cannot find your installed python. See if you need to edit some settings in VS Code for it to know what python executable to run. See
https://code.visualstudio.com/docs/python/environments for more detail.
It's probably because you used the command python without having an activated virtual environment or not using python3 or python3.6. Try it with python3.6 and see if that fixes it. If it doesn't then Python isn't on your PATH.
I have a specific problem with python. I have on my ubuntu two versions python3.4 and python3.6(from anaconda). I want use just anaconda pythoncurrent version
But when i run script i have some problems with another python version
another version
How i can safe delete useless python 3.4.6?
sudo apt-get remove python3.4
anaconda should have set python3 as default python3.6 version
You should probably not delete Python3 from your system, even if you have Anaconda installed, since there might be system software that:
was not tested with subsequent versions of Python;
might struggle finding Python from Anaconda.
What you should do instead is configure your IDE / environment to run Python script with Anaconda, e.g. by setting your PATH variable to point to your anaconda/bin directory or similar.
If you are using PyCharm, as it seems from the screenshot, you could set up your project to run the Anaconda Python without modifying your other command-line settings.
I'm interested in using Jupyter notebooks with both Python 2 and Python 3 (one of my colleagues insists on still using Python 2 ;) ).
So I diligently followed the steps listed in this excellent answer: Using both Python 2.x and Python 3.x in IPython Notebook.
I installed multiple kernels and now Jupyter notebooks has the option to use both Python 2 and Python 3!
However, I managed to somehow delete the Python[Root] kernel. Now, every time I open a notebook, it comes up with an error message and makes me choose between Python 2 and Python 3 kernel.
This is not the end of the world, but I'd like it to default to my Python[Root] kernel every time I open a new notebook. I use Anaconda by the way.
Thanks for the assistance!
The following assumes you've already created a python3 venv and you're using a windows system:
Open the command line and activate the python3 environment with:
activate <environment name>
On the same command line of step 1, install jupyter for python3:
pip install jupyter
Create a bat file on the folder containing the python3 notebooks:
jupyter.bat:
c:\<anaconda path>\envs\<environment name>\Scripts\jupyter-notebook.exe
Now just run jupyter.bat and it should open the default browser with all the python3 notebooks on the current folder.
I have not had time to fully digest the answer in the post you reference: Using both Python 2.x and Python 3.x in IPython Notebook -- but if what you currently have isn't working properly then what I would suggest is:
Install Anaconda if you haven't already (it sounds like you probably have done this).
conda update conda to update to the latest Conda (always a good idea)
conda install anaconda=4.1.1 to make sure you have the latest Anaconda (well, as of this date)
conda create -n ana41py27 anaconda python=2.7 to create a Python 2.7 based Conda environment that contains all the Anaconda packages
conda create -n ana41py35 anaconda python=3.5 to create a Python 3.5 based Conda environment that contains all the Anaconda packages
If you have any problems with those steps, report them here or on the Anaconda mailing list.
Once you have that in place you can start Jupyter notebook (any way you like, pretty much), and then you will be able to create new notebooks that are either Python 2.7 or Python 3.5 based by choosing the appropriate kernel from the "New" button:
or change between a Python 2.7 or Python 3.5 kernel from within a Notebook: