importing pygame on python3 on virtualenv cause ImportError - python-3.x

I have python3.3.1 on ubuntu lucid,which I invoke thru virtualenvwrapper ' .I wanted to learn pygame,so I used pip to install it.Before that I installed the sdl and smpeg dev libraries
me#ubuntu: sudo apt-get install libsdl1.2-dev
...
me#ubuntu: sudo apt-get install libsmpeg-dev
...
me#ubuntu: workon envpy331
(envpy331)me#ubuntu:~$ pip install pygame
Downloading pygame-1.9.1release.tar.gz (2.1MB): 2.1MB downloaded
Running setup.py egg_info for package pygame
WARNING, No "Setup" File Exists, Running "config.py"
Using UNIX configuration...
Hunting dependencies...
SDL : found 1.2.14
FONT : not found
IMAGE : not found
MIXER : not found
SMPEG : found 0.4.5
PNG : found
JPEG : found
SCRAP : found
PORTMIDI: not found
PORTTIME: not found
....
Continuing With "setup.py"
Successfully installed pygame
Cleaning up...
Then I tried to import pygame and this caused an import error
(envpy331)me#ubuntu:~$ python
Python 3.3.1 (default, Apr 19 2013, 11:41:37)
[GCC 4.4.3] on linux
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.virtualenvs/envpy331/lib/python3.3/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: /home/me/.virtualenvs/envpy331/lib/python3.3/site-packages/pygame/base.cpython-33m.so: undefined symbol: PyCObject_FromVoidPtr
Any idea how to correct this?

Pygame has not been entirely ported to Python 3, only some of the modules. If you use Python 2.7 everything should work. I recently had the same problem. Some suggested using individual modules of Pygame with Python 3 but this may be tricky to set up.

I know this is and old question but since I spent some time on the problem, this is the easiest way to install pygame for python3.4 in its own virtual environment:
sudo apt-get build-dep python-pygame
sudo apt-get install python3-dev libswscale-dev libavformat-dev virtualenvwrapper
echo "export WORKON_HOME=~/.venvs" >> ~/.bashrc
echo ". /usr/share/virtualenvwrapper/virtualenvwrapper.sh" >> ~/.bashrc
. ~/.bashrc
The above you do once. The following you repeat for each new environment:
mkvirtualenv -a ~/your/project/path -p /usr/bin/python3.4 your_project_name
pip install hg+http://bitbucket.org/pygame/pygame
This should download and compile the right pygame version (with bunch of warnings, yet working). Tested on Ubuntu GNOME 14.04 and 14.10.
Each time you start working on a given project or wish to switch between projects:
workon your_project_name
The project name will appear in the prompt. And when you want to finish and go back to your system-wide environment:
deactivate

Related

Unable to install bottle-memcache in ubuntu using pip3 with python3.6 [duplicate]

I have installed pip for python 3.6 on Ubuntu 14. After I run
sudo apt-get install python3-pip
to install pip3, it works very well. However, after installation, when I am trying to run
pip3 install packagename
to install a new package, something strange occurs:
File "/usr/lib/python3/dist-packages/pkg_resources.py", line 1479, in <module>
register_loader-type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module "importlib._bootstrap" has no attribute "SourceFileLoader"
It seems that I did nothing wrong, and I really cannot figure out the reason.
If you're getting this error running pip install dotenv, this is because the package is called python-dotenv not dotenv.
This worked for me:
sudo pip install python-dotenv
Faced the same problem. I think this is because python3.6 and pip3 were installed from different sources.
I suggest using python's inbuilt facility to install pip i.e
python3 -m ensurepip --upgrade
This should install pip3 and pip3.x where x in python3.x.
Same works for python2 also.
Tried to install Tensorflow in venv on Windows 10 machine with python 3.8 and got the same issue.
What helped for me was:
pip install setuptools --upgrade
I had the same problem on my ubuntu 18.04 with python 3.6. None of the above methods helped, but this one solved the problem:
pip3 uninstall setuptools
I had the same error whatever I asked to pip. I gave a look to this page: https://packaging.python.org/tutorials/installing-packages/
That line is the one that solved my problem:
python3 -m pip install --upgrade pip setuptools wheel
pip install setuptools --upgrade
This command solved my issue. It fixed my problem.
When updating from python3.4 to python3.6 on ubuntu 14.04. The following solved me:
wget https://bootstrap.pypa.io/ez_setup.py -O - | python3
If you face this issue in anaconda environment, simply upgrade setuptools using the following:
conda install -c conda-forge setuptools
That's because you are using an old version of setuptools, check up this issue.
I met the same problem, this is the key:
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3
I had faced the same issue on Ubuntu 19.10 and now I upgraded to Ubuntu 20.04 and faced the issue again. This issue is due to broken pip3. So whenever you enter pip3 and hit enter it will show the same error. So instead of using "pip3 uninstall setuptools" use the below code
python3 -m pip uninstall setuptools
It resolved my issue 3rd time
I am facing the same problem, which is solved by downloading the source files of the setuptools and installing the module manually.
The setuptools can be downloaded here:
https://pypi.org/project/setuptools/
After downloading, unzip the package first, then cd to the directory and run
python setup.py install --user
For me the error happened while trying to create a virtual env with python 3.8:
sudo virtualenv venv -ppython3.8
And after trying all the answers here, finally the problem solved by installing the new version of virtualenv (20.0.7):
sudo pip3 install virtualenv
I encountered this error message triggered by a slightly different situation which I'll mention here for anyone who finds this.
This same error also occurs when installing the distribute Python package (which is currently just a compatibility layer in front of setuptools) in Python 3.6 or newer.
In my specific situation I discovered this as I was using pyzmail which has been somewhat abandoned and depends on distribute.
Collecting distribute
Downloading distribute-0.7.3.zip (145 kB)
ERROR: Command errored out with exit status 1:
command: /var/lang/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ssqyqflj/distribute/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ssqyqflj/distribute/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-hgbjn0js
cwd: /tmp/pip-install-ssqyqflj/distribute/
Complete output (15 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-ssqyqflj/distribute/setuptools/__init__.py", line 2, in <module>
from setuptools.extension import Extension, Library
File "/tmp/pip-install-ssqyqflj/distribute/setuptools/extension.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "/tmp/pip-install-ssqyqflj/distribute/setuptools/dist.py", line 7, in <module>
from setuptools.command.install import install
File "/tmp/pip-install-ssqyqflj/distribute/setuptools/command/__init__.py", line 8, in <module>
from setuptools.command import install_scripts
File "/tmp/pip-install-ssqyqflj/distribute/setuptools/command/install_scripts.py", line 3, in <module>
from pkg_resources import Distribution, PathMetadata, ensure_directory
File "/tmp/pip-install-ssqyqflj/distribute/pkg_resources.py", line 1518, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
I had the same issue when I tried to install the Slate package in Windows 10, Python 3.7.4 version:
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
The instruction that generated the error:
C:\WINDOWS\system32>python -m pip install slate
The instruction that worked:
C:\WINDOWS\system32>python -m pip install https://github.com/timClicks/slate/archive/master.zip
I had the same problem with my cloud computer. If all above didn't work for you don't worry. Here's how I resolved it:
Download the pip file (pip-version.tar.gz) from:
https://pypi.org/project/pip/#files
For cloud, use this
curl https://files.pythonhosted.org/packages/8e/76/66066b7bc71817238924c7e4b448abdb17eb0c92d645769c223f9ace478f/pip-20.0.2.tar.gz --output pip.tar.gz
Extract the content of the file and cd into the directory.
Run the following in the directory
python3 setup.py install --user
You should have pip3 working without errors.
pip3 install setuptools --upgrade
This also cured the issue on Python 3.9 running on Windows 10 and even on a custom Docker image.
I was having the same issue while using docker-compose. This exact error can be caused by not being the root user. Running the same commands with sudo fixed it for me. I don't not have setup-tools installed, and I'm running pip3. My issue was that IntelliJ didn't have sudo rights, so i had to do it from the terminal.
Above solutions were not working in my case for Ubuntu 20
If you run the following command:
sudo apt install python3-setuptools
Then you might get the result:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-setuptools is already the newest version (45.2.0-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
But to actually solve it and re-install setuptools try following commands:
sudo apt download python3-setuptools
sudo dpkg -i python3-setuptools_45.2.0-1_all.deb
Note: Change the version of the python package that is downloaded
Traceback (most recent call last):
File "/usr/bin/pipenv", line 6, in <module>
from pkg_resources import load_entry_point
File "/home/myuser/.local/lib/python3.7/site-packages/pkg_resources.py", line
1479, in <module>
Mine looked pretty similar, but for some reason I had a python installation in my home directory .local folder.
I did some of the other answers in this thread to ensure I had good local copy of python, then did:
rm -rf ~/.local
Just go inside the /usr/lib/python3/dist-packages/, first copy setuptools file somewhere and then delete the setuptools. Everything will be okay for deleting you can use:
sudo rm -r setuptools
for copying
sudo cp -r setuptools /...Desktop/
After that if it gives errors just:
sudo pip3 install setuptools==3.8.1
to download again.

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

unable to import PySimpleGUI

I am trying to explore PySimpleGUI.
Following this link PySimpleGUI
But when I do,
import PySimpleGUI
getting error as,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/PySimpleGUI/__init__.py", line 2, in <module>
from .PySimpleGUI import *
File "/usr/local/lib/python3.7/site-packages/PySimpleGUI/PySimpleGUI.py", line 4, in <module>
import tkinter as tk
File "/usr/local/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 tried,
sudo apt-get install python3-tk
and
sudo apt-get install python3.7-tk
but could not get rid of the above mentioned error.
My current system details:
OS - Ubuntu 19.04
python - Python 3.7.3
PySimpleGUI - 3.29.0
tcl - 8.6.9
How to solve this issue?
Now I am able to solve the issue, for that I used pyenv and install python through pyenv.
I used the following steps:
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
sudo apt update && sudo apt upgrade
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev git
Add to ~/.bashrc
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Reload bashrc
source ~/.bashrc
Install python latest version
pyenv install 3.7.0
list python versions
pyenv versions
set global version
pyenv global 3.7.0
check python version
python -V
install PySimpleGUI
pip3 install PySimpleGUI
That's it, now I am able to import PySimpleGUI.
import PySimpleGUI
tk is already in python - you don't need to install it.
You can try these steps:
- reinstall the python3
- (on UNIX* OS) use pip3, not pip
- pip3 install PySimpleGUI
- pip3 install --upgrade --force PySimpleGUI
You can try this to list all packages under pip. Tkinter should be installed by default and PySimpleGUI should show up in the list.
python -m pip list
I solved this problem by using Conda
I uninstalled python and installed python through Conda. Then I created a new project that uses Conda as the env and simply chose which version of Python I wanted, and then it worked.

virtualenv: cannot import name 'main'

I'm having a little trouble with virtualenv on Mac OS X Yosemite. After I couldn't run virtualenv at all first, I installed Python 3 via brew (previously I installed it via the package on python.org). I linked this installation of python3, updated pip and ran pip3 install virtualenv. When I try to run virtualenv (e.g. $ virtualenv --python=python3 ../virtualenv), I get the following error message.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv import main
File "/usr/local/bin/virtualenv.py", line 7, in <module>
from virtualenv import main
ImportError: cannot import name 'main'
Can anybody help me with this?
After my upgrade to Fedora 32 I had the same issue which lead me to this question:
ImportError: cannot import name 'main' from 'virtualenv'
In my case I actually seemed to have both /usr/local/bin/virtualenv as well as $HOME/.local/lib/python3.8/site-packages/virtualenv/__init__.py.
Removing the user virtualenv version and reinstalling it into the system with root fixed the issue:
pip uninstall virtualenv
sudo pip install virtualenv
Your virtualenv executable /usr/local/bin/virtualenv is importing the virtualenv package /usr/local/bin/virtualenv.py. My guess is that package is not the one the executable should really be importing. The reason it is choosing that one is because it is in the same directory.
First, check where the real virtualenv package is. In the python3 terminal:
>>> import virtualenv
>>> virtualenv.__file__
If it is not /usr/local/bin/virtualenv.py, then the simplest way to get /usr/local/bin/virtualenv to import it instead of /usr/local/bin/virtualenv.py is to delete /usr/local/bin/virtualenv.py (or so you can easily undo this if it doesn't work, simply rename virtualenv.py to something else like xvirtualenvx.py).
I received this error after upgrading Ubuntu 18.04 LTS to 20.04 LTS. So there were two problems all at once. First the python version was still running 2.x and doing a simple update or try to uninstall (apt-get remove virtualenv) of virtualenv did not help at all. But I found a solution. First let 20.04 LTS 'know' the times of using old python is over:
sudo apt-get install python-is-python3
Then test it and open a console to get the version string with python -V; by now it should be showing something like Python 3.8.5. Fine.
Next step is to solve the virtualenv problem. I tried to find out, which executable was run with which virtualenv and it showed: $HOME/.local/bin/virtualenv. Hmmkay, somehow the system wasn't using the /usr/bin/virtualenv executable. I thought maybe I let the directory become invisible (a.k.a. renaming) and maybe the system will go on a hunt for an alternative virtualenv running:
mv $HOME/.local/bin/virtualenv /home/USER/.local/bin/virtualenv_OLD
Then I simply changed into a playground-directory and ran virtualenv donaldknuth and behold - it worked. To be sure I ran another which virtualenv and the system returned a /usr/bin/virtualenv. Last check to do was activating the new virtual environment:
source $HOME/playground/donaldknuth/bin/activate
The terminal changed and it worked fine. Solution
EDIT:
Based on Pierre B.'s suggestion you may have to restart your Shell. The command hash -d virtualenv will delete the stored location of virtualenv from the shell's cache and determine the correct path right now. (Sources: https://www.computerhope.com/unix/bash/hash.htm, https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables)
Similarly to some others here, I had multiple installations of virtualenv. Not sure where the extra one came from, but I had these two:
/usr/local/bin/virtualenv
/usr/bin/virtualenv
One is from apt install of virtualenv, the other from pip install of virtualenv.
This happened when upgrading to Ubuntu 20.04.
On Linux Mint 20, I had to switch default Python interpreter to python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Then remove existing virtualenv and reinstall via pip and python3:
rm ~/.local/bin/virtualenv
apt remove python3-virtualenv
sudo pip install virtualenv

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