Related
I'm new to conda and seeing something strange but I don't actually know if it's a problem or not.
I'm currently in the root environment. At some point I was trying to install pip in another environment, but accidentally just ran pip install requests. This seems to have installed it in my root environment:
$ conda list | grep requests
requests 2.12.4 py36_0
requests 2.13.0 <pip>
And it looks like the pip version is what's getting picked up when I run python:
$ python
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests; requests.__version__
'2.13.0'
My guess is that having two versions of the same package lying around is going to cause headaches later. Then my assumption was that I'd be better off keeping the non-pip version, so I tried this:
$ pip uninstall requests
[asks for confirmation, say yes]
$ conda list
[traceback, which I can post if helpful. Summary is:]
ModuleNotFoundError: No module named 'requests'
Then pip install requests brings me back to square 1 (having both versions of requests).
Finally, I want to know how to prevent this from happening again. According to the docs, if I want to use pip to install a package in a conda environment, I should:
Activate the conda environment where you want to install the package
run pip install whatever
It should show up in conda list for the current environment.
However, this isn't working for me - the installed package shows up under conda list --name root rather than in the current environment.
So, handful of questions:
Is it a problem to have two copies of requests in my conda root?
If this is a problem, how do I fix it?
How do I use pip within a conda environment?
Is it a problem to have two copies of requests in my conda root?
Probably.
If this is a problem, how do I fix it?
In my testing, conda remove followed by pip uninstall does the trick. (After which you can just re-install requests using only conda this time.) But if something goes wrong, remove .../lib/python3.6/site-packages/requests-2.13.0.dist-info. That seemed to work for me.
FWIW, I was only able to reproduce the double-install by installing with pip first, then installing again with conda.
How do I use pip within a conda environment?
Your summary in the OP is correct. Just activate the conda environment and use pip as you normally would. My rule of thumb is to install packages with conda if they are available, and resort to pip otherwise.
I tried to use the matplotlib package via Pycharm IDE on windows 10.
when I run this code:
from matplotlib import pyplot
I get the following error:
ImportError: No module named 'tkinter'
I know that in python 2.x it was called Tkinter, but that is not the problem - I just installed a brand new python 3.5.1.
EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).
For Linux
Debian based distros:
sudo apt-get install python3-tk
RPM based distros:
sudo yum install python3-tkinter
For windows:
For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.
Test the python module
python -c "import tkinter"
p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.
you can use
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
if you dont want to use tkinter at all.
Also dont forget to use %matplotlib inline at the top of your notebook if using one.
EDIT: agg is a different backend like tkinter for matplotlib.
For Windows users, there's no need to download the installer again. Just do the following:
Go to start menu, type Apps & features,
Search for "python" in the search box,
Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
On the Modify Setup page click Modify,
Tick td/tk and IDLE checkbox (which installs tkinter) and click next.
Wait for installation and you're done.
On Centos, the package names and commands are different. You'll need to do:
sudo yum install tkinter
To fix the problem.
Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:
import tkinter
import _tkinter
tkinter._test()
If you are using fedora then first install tkinter
sudo dnf install python3-tkinter
I don't think you need to import tkinter afterwards
I also suggest you to use virtualenv
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And add the necessary packages using pip
On CentOS 7 and Python 3.4, the command is sudo yum install python34-tkinter
On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter
For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"
On Ubuntu, early 2018, there is no python3.6-tk on ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won't work.
My solution was to use set everything up with python 3.5:
sudo apt install python3.5-tk
virtualenv --python=`which python3.5` python-env
source python-env/bin/activate
pip install -r requirements.txt
And now matplotlib can find tkinter.
EDIT:
I just needed 3.6 afterall, and the trick was to:
sudo apt install tk-dev
and then rebuild python3.6, after tk-dev, eg:
./configure
make
make install
If you are using python 3.6, this worked for me:
sudo apt-get install python3.6-tk
instead of
sudo apt-get install python3-tk
Which works for other versions of python3
For the poor guys like me using python 3.7. You need the python3.7-tk package.
sudo apt install python3.7-tk
$ python
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()
Note. python3-tk is installed. But not python3.7-tk.
$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...
After installing it, all good.
$ python3
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()
On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter
Sometimes (for example in osgeo4w distribution) tkinter is removed.
Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
Since I'm using Python 3.7 on Ubuntu I had to use:
sudo apt-get install python3.7-tk
Maybe you installed python from source. In this case, you can recompile python with tcl/tk supported.
Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I'll suppose you installed python at /home/xxx/local/tcl-tk/.
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.
cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install
I had the same issue on Win x86/64 because my custom Python3.7 installation did not include Tcl packages, so just modify or re-install your python
https://www.python.org/downloads/release/python-370/
Download Python Setup file and click modify then tick the tcl/tk and install.
After installation is complete go to folder where python is installed ( Default is C:\Users*Your username*\AppData\Local\Programs\Python\Python39\Lib) .
Copy the tkinter folder and paste it in the lib folder of your pycharm project.
Error should be resolved
Follow these steps to easily install Tkinter on your PyCharm IDE:
First go to the File:
Second is go to New Project Setup > Settings for new projects:
And then click the Settings for new projects and you'll get redirected in here:
Please click the + symbol in there:
after that install future and there you go...
If you’re having pip(which you probably do), open up cmd or powershell on Windows or a terminal window on OS X or Linux and try this(make sure python is in the system path if you’re on Windows):
pip install tkinter
It should take a while to install tkinter, and then try to execute this code block:
from tkinter import *
root = Tk()
# Your code goes here
root.mainloop()
Hope that this helps! Thank you!
I've installed Anaconda with the pkg installer:
Python 2.7.10 |Continuum Analytics, Inc.| (default, May 28 2015, 17:04:42)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
but when I attempt to use anything from matplotlib, i.e.:
from matplotlib import pyplot as plt
I get
RuntimeError: Python is not installed as a framework.
The Mac OS X backend will not be able to function correctly if Python is not installed
as a framework. See the Python documentation for more information on installing Python
as a framework on Mac OS X. Please either reinstall Python as a framework,
or try one of the other backends.
I'm really not sure what this means, or how to go about fixing it.
Posting since I just had this issue and this was a quick fix:
If you used pip to install:
Create ~/.matplotlib/matplotlibrc
Add "backend: TkAgg" (without the quotations) to the file.
I was having the same problem with anaconda 2 & matplotlib 1.5.3.
Running a simple conda install matplotlib to reinstall matplotlib did the trick for me.
If the problem is only matplotlib, is worth try to change the backend:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
If it works you can change the backend permanently from the matplotlibrc file.
I was having the same problem. Installing an older version of matplotlib did the trick for me. Try this command in your terminal while in your virtual environment:
pip install matplotlib==1.4.3
Run the file using pythonw instead of python.
This happens because python is not installed as a framework.
Therefore use pythonw myScript.py instead of python myScript.py
I am sure this will fix it.
I had a similar error.
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
From the matplotlib documentation;
$ conda install python.app
You need a framwork build of Python for matplotlib, but
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python
NB I had to add the conda-forge channel as python.app isn't included in the default miniconda channels
$ conda config --add channels conda-forge
If you experience this error, don't forget to check your bash_profile.
You can do this in terminal by:
cd
then
nano .bash_profile
check the contents. Macports and Homebrew add their own headings for things they've done here. You can remove the declarations they make to $PATH. Just leave the one Anaconda has made. I had a If you would like, you can:
cp .bash_profile ./bash_profile_backup_yyyy_mm_dd
and have a backup of the file, with filename indexing to the date you changed it. That is, provided you actually put in the date in instead of just the formatting characters I'm suggesting.
source ~/.bash_profile
will refresh your system's reference to the bash_profile and you should be good to go in importing and using matplotlib
if using inside a virtualenv, I recommend following the instructions here:
http://matplotlib.org/faq/virtualenv_faq.html
A reinstall of matplotlib should fix the issue for you as it did for me with
conda install matplotlib
Quickfix: Run your file using pythonw, instead of python.
e.g pythonw testFile.py.
I wrote a script some times ago that contain
from lxml import etree
But, unfortunatly it is not working anymore.
In doubt i checked installation with :
sudo apt-get install python-lxml
sudo pip install lxml
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
I checked if it could be my python version with :
me#pc:~$ python
Python 2.7.3 (default, Sep 14 2012, 14:11:57)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named lxml
My os is ubuntu 12.04.1 LTS with Python 2.7.3.
All seems fine. I can't see what could be the problem.
Your solution cited in edit, which use the xml.etree instead of lxml.etree is not the better way to do it, as these module have known incompatibilities, and mainly because lxml is certainly more optimised.
A good way to make a clean environment available is to use virtualenv :
$ virtualenv myproject
$ cd myproject
$ ./bin/pip install lxml # Repeat this with other dependencies
[wait for download and compiling]
Then, use ./bin/python to execute your script.
The advantages of this method are :
you can have different versions of dependencies between your system and your project
even if you break everything in your virtual environment, you will not compromised the rest of your system
you do not need root privileges to make an installation
As a reference, a more powerful but slightly more complex way to achieve this is to use buildout, but it can looks like hunting flies with a bazooka if you just want to execute a simple one-file script.
Solved the problem.
It seems that a software that i installed messed out my python path. The python that i was using when calling python in a terminal was the one installed by the software, and the one called by my script was the one installed on my system.
So, i just removed the python path of the software from the path variable of bash.
I had this problem when running tests in PyCharm. I could import lxml when using the terminal and virtual environment, but using the same virtual environment to run the tests resulted in the ImportError.
In my case upgrading from lxml 3.3.5 to 4.2.5 fixed the problem.
This likely has something do to with your Python environment. Here are three troubleshooting methods, from least to most cumbersome
1) Re-install via pip
pip uninstall lxml
pip install lxml
2) Re-Install via IDE
uninstalling it from the terminal (pip uninstall lxml)
adding from lxml import etree at the beginning of a python file
letting my IDE (PyCharm) show a missing import error
hovering over the error and installing it from the IDE via the error-fixing pop-up (not terminal)
3) Re-create virtualenv
Assuming you're working in a virtual environment (if not, shame to you and your family): delete and create a new environment.
if youre dealing with conda, i made the "discovery, that my conda install lxml does not work propperly after "successfully" installing
try uninstalling and reinstalling with pip, that worked for me
conda remove lxml
pip install lxml
For Python 3.6, it is very tricky.
I have the same problem and try to install the latest version, lxml 3.8. But I still get the error. And then I use lxml 3.7.3, then it is ok.
Tab completion on IPython seems not to be working. For example,
import numpy
numpy.<tab>
simply adds a tab.
import numpy
num<tab>
just adds a tab, too. Could you please suggest some possible causes for this problem? I am running Windows 7 and Python 2.6.5.
Be sure you have installed the pyreadline library. It is needed for tab completion and other IPython functions - in Windows it doesn't come with the IPython package and you have to install it separately -
> pip install pyreadline
In case anyone is using the recent 7.19.0 and autocomplete does not work, try downgrading jedi to 0.17.2:
pip install jedi==0.17.2
See https://github.com/ipython/ipython/issues/12740 for details.
pip uninstall jedi --yes
and
pip install pyreadline
The current Ipython with the Jupyter notebook doesn't require jedi.. So you have to just uninstall it with the above command.
I got it from here.
pip told me I had pyreadline version 1.7.1 installed
C:\Users\me>pip freeze | grep readline
pyreadline==1.7.1
Upgrading pyreadline fixed it for me:
C:\Users\me>pip install --upgrade pyreadline
C:\Users\me>pip freeze | grep readline
pyreadline==2.0
Your ipythonrc file may be out of date.
Try running
ipython -upgrade
Downgrading iPython did the trick.
pip install --upgrade ipython==5.8.0
I had this problem.
I solved by downgrade the python-parso package
downgrading the python-parso package (0.8.0-1 => 0.6.2-1)
This should definitely work as it worked in my case
conda install ipython
pip install jedi==0.17.2
The classic 'have you tried turning it off and on again' worked for me.
pip uninstall ipython
pip install ipython
As of right now, on a OSX, pip installed ipython doesn't give tab completion, pyreadline release.py is busted .. what WFM:
easy_install ipython readline
YMMV.
Someone else in StackOverflow posted this link: http://www.vankouteren.eu/blog/2009/06/getting-ipython-readline-and-auto-completion-to-work-on-mac-os-x/
Its basicly easy_install readline than discover where the readline egg got installed and edit the ipython bin script to use this readline:
Install the "official" readline: easy_install readline
Discover where it is. Look at /Library/Python/site-packages/readline-*.egg or in your Virtualenv counterpart
Discover where ipython bin is: which ipython
Add ONE LINE to this file, adding the readline egg path right after import sys line.
My virtualenved ipython bin script got working as follow:
#!/Users/alanjds/src/git/cervejeiras/venv/cervejeiras-lfs/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'ipython==0.13.1','console_scripts','ipython'
__requires__ = 'ipython==0.13.1'
import sys
### ONLY LINE ADDED:
sys.path.insert(0, '/Users/alanjds/src/git/cervejeiras/venv/cervejeiras-lfs/lib/python2.6/site-packages/readline-6.2.4.1-py2.6-macosx-10.6-fat.egg')
####
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('ipython==0.13.1', 'console_scripts', 'ipython')()
)
I realize this is a really old question, but none of the answers above worked for me (And this is the first hit you get when you google a question of this nature).
I should mention that this is NOT exclusive to windows, I had the problem running CentOS 6.5 and Python 2.7
Here is what I did:
apt-get/yum install ncurses-devel
#If you want history in iPython:
apt-get/yum install sqlite-devel
easy_install ipython readline
ipython
In [1]: from
Display all 391 possibilities? (y or n)
If you don't have the -devel packages, your install will fail when it comes time to link them and build the eggs.. Hope this helps others!
I had this problem and knew that I had the pip installed for the module I was looking for. Performing $ ipython --init solved the problem for me.
I had to mv ~/.ipython{,.bak} in my case.
If you use Jupyter notebook and you still did get Tab auto-complete working after you tried all the steps suggested in the post here, you might want to check if you are trying to use the Tab auto-completion within a function definition. Ifyour import statements are part of the function such as below, you will not get the Tab auto-completion. You need to put the import statements outside the function and also execute them once before asking for auto-completion on the packages.
def myfunction():
import pandas as pd
import numpy as np
a = pd.DataFrame(np.random.normal(1,3, (4,4))
return a
I faced the same problem with the numpy library. The issue is with the particular version of ipython or jupyter notebook and it is resolved by simply updating ipython or jupyter.
If you are using a conda environment like anaconda or miniconda then update ipython in that environment by using
conda update ipython
In case of anaconda you also need to update the qtconsole
conda update qtconsole
Sometimes anaconda constraints the update of ipython then try
conda update -all
If you are not using a environment then directly update using pip
pip update ipython
I solved my issue by installing jedi-language-server:
pip install -U jedi-language-server
PS, I was installed Ipython from Conda in a virtual env and used the above command when the env was activated.
To check if ipython and the modules it uses match, run pip check ipython.
For my configuration with ipython 7.25 in July 2021, this gave a good clear warning:
ipykernel 6.0.1 has requirement importlib-metadata<4; python_version < "3.8.0", but you have importlib-metadata 4.6.0.
You may of course see different warnings --
this is just an example, unrelated to tab completion.
Then to downgrade it, e.g.
pip install 'importlib-metadata<4' # don't forget the 'quotes'
Successfully uninstalled importlib-metadata-4.6.0
Successfully installed importlib-metadata-3.10.1
Also useful:
pip list -- everything in your $PYTHONPATH, with version and location
pip check -- everything
pip show ipython --
Requires: traitlets, pygments, jedi, decorator, pickleshare, pexpect, appnope, matplotlib-inline, setuptools, prompt-toolkit, backcall
but to see the required versions you have to look at
.../site-packages/ipython-*.dist-info/METADATA
Pyreadline is needed by ipython. Install pyreadline. This was done in Windows 7. Get pyreadline zip, pyreadline-master.zip, unzip. In powershell change directory into uzipped pyreadline, make sure python is set in Path, and enter commandpython setup.py install This will intall pyreadline in C:\Python27\Lib\site-packages