Unexpected version discrepancy with VSCode, pip, venv and terminal - python-3.x

I'm using the current version of VSCode on Ubuntu 22.04.1, Python 3.11.1 and venv. I installed many packages and never seen this problem before. In this environment:
I installed a package with pip install clipspy, which completed successfully and pip lists the package as clipspy 1.0.0. Running a test script inside VSCode produces:
(.venv) paul#desktop:~/work/arc/code$ cd /home/paul/work/arc/code ; /usr/bin/env /home/paul/work/arc/code/.venv/bin/python /home/paul/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 36033 -- /home/paul/work/arc/code/clips-0.py
Traceback (most recent call last):
File "/home/paul/work/arc/code/clips-0.py", line 1, in <module>
import clips
ModuleNotFoundError: No module named 'clips'
(.venv) paul#desktop:~/work/arc/code$ python3 --version
Python 3.11.1
Strangely, when I open a terminal and activate the same virtual environment with Python 3.10.6
(.venv) paul#desktop:~/work/arc/code$ python3 clips-0.py
Hello John Doe
(.venv) paul#desktop:~/work/arc/code$ python3 --version
Python 3.10.6
It runs fine. It fails with Python 3.11.1, though. Can someone explain what is happening here? How can I make this module run with Python 3.11?
I created a new VSCode project and venv, repeated the installation and it works as expected now. The previous project had well over 100 modules installed. Is it likely that venv environment got corrupted somehow?

Related

Imported module was not found?

I am building a web scraper and I am trying to import the 'requests' package but I am getting an error. I am being told the following:
ModuleNotFoundError: No module named 'requests'
Full Error:
(venv) USERs-MacBook-Pro:Scraper user$ /usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
Traceback (most recent call last):
File "/Users/user/git/ML/Python/Practice/Scraper/Scraper.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Steps I took when setting up project:
python3 -m venv project_name/venv
source project_name/venv/bin/activate
python3 -m pip install SomePackage
Confirmation package and dependences were installed:
(venv) USERs-MacBook-Pro:Scraper user$ pip list
Package Version
---------- ---------
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 20.2.3
requests 2.25.1
setuptools 49.2.1
urllib3 1.26.2
By using the absolute path to system Python like that:
/usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
you are using system's Python 3.9 and the packages that are installed for that one although you are in a virtual environment.
When creating a virtual environment you are creating a separate environment with the specified python version and all the packages you install with pip are installed to this environment and this python version.
You can better understand that if you run:
which python
inside your virtual environment.
This will show you the python that is used when you run python inside your venv and it's going to be different than
/usr/local/opt/python#3.9/bin/python3.9
So, by having installed requests using pip inside your environment, it is installed in the environments python which is executed when you run just python.
To sum up, to use the packages installed inside your venv with pip you should run your script (after activating the venv) with:
python /Users/user/git/ML/Python/Practice/Scraper/Scraper.py

Python/Tkinter : ModuleNotFoundError: No module named '_tkinter'

This is my first post StackOverflow, I will try to make it as correct and complete as possible if you have any tips to improve my post I will gladly accept it.
I'm having trouble running code written in Python that uses Tkinter.
I will try to describe in detail my actions to facilitate the identification of the error.
I started a course at Coursera on DSP (Digital Signal Processing) where it is suggested to install a tool written in python (and a little bit of C). I'm using Arch Linux.
link on Github:
sms-tools repo
Using pyenv/virtualenv/virtualenvwrapper I created an environment with Python 3.7.5, as recommended in the "How to use" section of the repository.
I installed the required libraries in my environment by pip:
%pip install ipython numpy matplotlib scipy cython
I compiled some C functions in the "/sms-tools/software/models/utilFunctions_C"
directory with the following command:
%python compileModule.py build_ext --inplace
Finally, I run the models GUI in the directory "/sms-tools/software/models_interface"
%python models_GUI.py
and I get the following message:
Traceback (most recent call last):
File "models_GUI.py", line 6, in <module>
from Tkinter import * ## notice capitalized T in Tkinter
ModuleNotFoundError: No module named 'Tkinter'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "models_GUI.py", line 9, in <module>
from tkinter import * ## notice lowercase 't' in tkinter here
File "~/.pyenv/versions/3.7.5/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I will now describe some of my attempts to solve the problem:
Looking at Tkinter section in Python Wiki I tried installing Tcl and Tk.
%sudo pacman -S tk
but it was already installed. after that I tried installing with pip:
%pip install tk
and
%pip install tkinter
and the error remains the same.
I also tried to create a symlink with this code:
%ln -s /usr/lib/python3.8/lib-dynload/_tkinter.cpython-38-x86_64-linux-gnu.so _tkinter.cpython-38-x86_64-linux-gnu.so
the symlink was created in the following folders:
~/.ve/Coursera_DSP/lib/python3.7/lib-dynload
and
.pyenv/versions/3.7.5/lib/python3.7/lib-dynload
But I still get the same error.
I appreciate it if anyone has any suggestions and I apologize for the language errors since English is not my mother tongue.
After an incessant search on the internet, I believe the problem is related to pyenv and TCL/TK.
I don't understand much about the subject but I suspect that in the creation of the environment by virtualenv python has lost the connection with TCL/TK. Does that make any sense?
Here is step by step guide to make IDLE and tkinter work. Working for me on macOS Catalina. Should be easily adapted to Linux environment:
install tcl-tk with Homebrew. In shell run brew install tcl-tk
in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
reload shell by quitting Terminal app or run source ~/.zshrc
after reloaded check that tck-tk is in $PATH. Run echo $PATH | grep --color=auto tcl-tk. As the result you should see your $PATH contents with tcl-tk highlighted
now we run three commands from Homebrew's output from step #1
in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
in shell run export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall <your python version>. E.g. pyenv uninstall 3.8.2
set environment variable that will be used by python-build. In shell run PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
Note: in future use tck-tk version that actually installed with Homebrew. At the moment of posting 8.6 was the actual
finally install Python with pyenv with pyenv install <version>. E.g. pyenv install 3.8.2
Test
in shell run pyenv global <verion that you've just installed>
now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".
now check tkinter. In shell run python -m tkinter -c "tkinter._test()". You should see test window like on the image:
That's it!
My environment:
check this is something went wrong executing steps above:
macOS Catalina
zsh (included in macOS Catalina) = "shell" above
Homebrew (installed with instructions from Homebrew official website)
pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub)
Python 3.8.x - 3.9.x (installed with pyenv install <version> command)
mac Monterey (M1)
brew install python-tk
python 3.9.10
My basic install of linux mint 20 contains python3.
On windows machines this includes the tkinter package.
My solution was to
sudo apt-get install python-tk
or
sudo apt-get install python3-tk
and live was sunny again.
I created a venv using Python 3.10.5 (alternate version to default 3.8). Tkinter failed to install when I used the command "sudo apt-get install python3-tk"; however, when I used "sudo apt-get install python3.10-tk", the module installed properly. Apparently, using the specific python3 version made the difference.
Since I couldn't find any solution I just uninstalled my whole environment system (pyenv pyenv-virtualenv virtualenv and virtualenvwrapper) and installed conda instead. Now everything works.
Probably I messed up with pyenv installation.
Thank you all :D

Install spyder independently(without Anaconda, python x,y) on windows 10

I was trying to install spyder on windows 10 with the python version 3.6+. But it was not working. It was giving me the following error. Seriously, i don't know what these error means. Error:
An error ocurred while starting the kernel
Traceback (most recent call last):
File "c:\python36\lib\site packages\spyder\utils\ipython\start_kernel.py", line 269, in
main()
File "c:\python36\lib\site packages\spyder\utils\ipython\start_kernel.py", line 237, in main
from ipykernel.kernelapp import IPKernelApp
File "c:\python36\lib\site packages\ipykernel\__init__.py", line 2, in
from .connect import *
File "c:\python36\lib\site packages\ipykernel\connect.py", line 13, in
from IPython.core.profiledir import ProfileDir
File "c:\python36\lib\site packages\IPython\__init__.py", line 41, in
""")
ImportError:
IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Beginning with IPython 6.0, Python 3.3 and above is required.
See IPython `README.rst` file for more information:
https://github.com/ipython/ipython/blob/master/README.rst
I just deleted all the previous version of the python from the C drive manually.
then, install the python 3.6.5(latest stable version available) at the time of posting.
Sometimes i faced issues in installing python and access it through windows cmd.
To resolve this, during the installation of python 3.6.5, opt for customized installation and when it ask for the folder in which to install it.
just create a folder in C drive with name Python36 and install python in this folder. Means install python in C:\Python36
and add two paths to the Path variable using the following commands.
setx path "%path%;C:\Python36;"
setx path "%path%;C:\Python36\Scripts;"
and then, try python.
To check, just type python --version
if it is working, python is ready to use
Then, i tried to install SIP package from pip. but pip was not working.
then, i manually install and upgrade the pip on my system using the following commands windows command line,
python -m pip install pip==9.0.0 # will install pip
pip install -U pip # will upgrade pip
pip got installed successfully and the version i checked was
pip 9.0.3 using the following command on windows command line:
pip --version
After that just run the following commands from cmd:
pip3 install SIP
pip3 install PyQt5
pip install spyder
Then, to check either spyder is working or not, just go the directory in which python is installed. e.g. on my sysytem, directory is C:\Python36\Scripts. search for spyder3app and click on it. and create a shortcut for it.
Versions:
PyQt5 (5.9.2)
sip (4.19.8)
Install the spyder using pip:
pip install spyder

python3 - No module named 'html5lib'

I'm running a python3 program that requires html5lib but I receive the error No module named 'html5lib'.
Here are two session of terminal:
sam#pc ~ $ python
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
[GCC 4.9.2] on linux2
>>> import html5lib
>>> html5lib.__file__
'/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
>>> quit()
sam#pc ~ $ python3
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
>>> import html5lib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'html5lib'
>>>
Where can be the problem?
Seems you have the module only for python 2. Most probably need to install it for python3. Usually use pip3 for that.
pip3 install html5lib
You can check your installed modules using:
pip freeze (or pip3 freeze)
I strongly recommend you to use virtualenv for development. So you can separate the different python versions and libraries/Modules by project.
use:
pip3 install virtualenv
You can then easily create "environments" using (simple version)
virtualenv projectname --python=PYTHON_EXE_TO_USE
This creates a directory projectname. You just switch into that dir and do a
Scripts\activate (on linux/unix: source bin/activte)
And boom. You have an isolated environment with the given python.exe and no installed modules at all. You also have an isolated pip for that project. Really helps a lot.
To end working in that project do a:
Scripts\deactivate (on linux: deactivate)
Thats it.
ONe moer thing ;) You can also do a
pip freeze > requirements.txt
to save all needed dependencies for a project in a file.
Whenever you need to restart from scratch in a new virtualenv you cabn simply do a:
pip install -r requirements.txt
This installs all needed modules for you. Add a -U to get the newest version.

pip3 works with python3.2 but won't work with python3.3

I have created a virtual environment for python3
virtualenv -p /usr/bin/python3 myenv
Everything works ok and pip installs python3 packages. Then I need to upgrade my python3.2 to python3.3. I am using ubuntu 12.04, so I need to do what Luper Rouch suggests here through ppa
I copy /usr/bin/python3.3 into myenv/bin/python3, thus (inside myenv):
python --version
returns Python 3.3.5. But then pip stops working failing with
pip install urllib3
Traceback (most recent call last):
File "mypathtoevn/bin/pip", line 7, in <module>
from pip import main
ImportError: No module named 'pip'
Then I understand that I have to use
pip3.3 install <package_name>
but I can't find any other pip*.* version either globally or inside virtualenv in my system.
How can I get pip3.3 or any other pip version?
Thanks
The ppa you have installed does not contain pip. The python package installer is itself installed with easy_install. On the question you linked to follow the later parts of this answer
Basically, calling
wget http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
should be sufficient. You should use the easy_install command of your Python3.3 install.
Check with
which python
where your python comes from, and search for easy_install in that path. In your case you know where it resides: Your virtualenv.

Resources