Conda showing two versions of requests library - python

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.

Related

Unexpected behavior after conda force reinstall

After downgrading numpy from version 1.22.4 to 1.19.5 with the following command-
conda install numpy==1.19.5 --force-reinstall
Python behaves unexpectedly. For example, pip list and conda list both show that the version of numpy remains 1.22.4, yet when starting a shell, the numpy version shows up as 1.19.5:
(tensorflow) demo % python
Python 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:24:38)
[Clang 12.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.19.5'
I have tried deactivating and activating the conda environment. There are no PYTHONPATH environmental variables set, nor is there a module called numpy.py in my directory. What could explain how the numpy version imported in the shell does not correspond to the version displayed by conda list / pip list in the terminal?
Occasionally, I have found that Python can conflict with packages, especially with bigger frameworks, as fully uninstalling it can be difficult. That's the beauty of virtual Python environments, you are able to just create a new one for specific versions, and not have to deal with multiple versions on your native Python install.
It might be worth using another package manager, such as Mamba (https://github.com/mamba-org/mamba), which I have had better experiences with. It also has the benefit of being multi-threaded, and more efficient.
Otherwise, your best bet would probably be to create a new virtual environment, which you can do via (be sure to change the python version if you have a specific use-case, or just get rid of the flag to have the latest version all the time):
conda create --name your-name-here python=3.9 -y
If you are using Mamba, you can replace any instance of 'conda' that you would otherwise use, for example to install your specific package:
mamba install numpy==1.19.5
Also, I would recommend using 'Miniconda', as I have had a much better experience with it, just make sure to 'activate' your specific virtual python environment.

Python not finding modules

Even after installing selenium using pip on Python 3.6.3, whenever I try to run a code with import selenium I get the message that ModuleNotFoundError: No module named 'selenium'.
I usually use Anaconda Prompt and run my codes in Jupyter notebook, but I made the installation also in regular cmd.
Does anyone have an idea about how to solve this?
From your command prompt you can run:
$ conda list
This will list all of the packages in your environment.
Ensure that your system is running the anaconda flavor of python.
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
To ensure the uniformity of package management you could use:
$ conda install -c conda-forge selenium
Edit: As you can see the path at which the "Requirement is already satisfied" is not the path in which Anaconda is installed.
You may or may not get a message saying that selenium is already installed. In that case, you may just need to ensure that you are in the conda environmnet—either a virtual conda environment or running right off the anaconda installation.
If you use the Anaconda GUI and you can install packages through this, I would try to do so. The point of virtual environments is to void these issues. I would bet importing selenium at the command prompt works, however, trying to use it within a Jupyter Notebook fails—because it is outside the environment, virtual or otherwise.
Updated:
Installation and testing of installation:
windows
macos
linux - the linux people know what's up.
I think you have both python 2.x and python 3.x installed on your system.
When you do pip install selenium, the module gets installed for python 2.x.
To install the module for python 3.x, use pip3 install selenium.

Install a newer Python when there are installed packages linked against the old one

(Please bear with me for the long description, it has been quite a troubleshooting journey.)
My ultimate goal is to get python package graph_tool working on my system and also on ipynb if possible. I have already brew install graph-tool, as indicated here, but that's still insufficient.
So I follow conda instructions here, and try to fulfill all requirements including having cgal and python3.6. I had anaconda with python2.7 and python3.5 originally, but since python3.6 is necessary, I download anaconda3 with python3.6. (conda update python still kept giving me 3.5)
I delete /Users/mymacbook/anaconda, so that /Users/mymacbook/anaconda3 would the default search directory. And I have a mini-success!
$ python3
Python 3.6.2 |Anaconda, Inc.| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Unfortunately, as I continue to conda install the other packages required (boost, cgal, etc), my python3 is reverted back to 3.5 :(
$ python3
Python 3.5.4 |Anaconda custom (x86_64)| (default, Oct 5 2017, 02:58:14)
$ conda install python=3.6
Fetching package metadata ...........
Solving package specifications: .
UnsatisfiableError: The following specifications were found to be in conflict:
- cgal -> python 3.5*
- python 3.6*
Use "conda info <package>" to see the dependencies for each package.
$ conda info cgal
Fetching package metadata ...........
ResolvePackageNotFound:
- cgal
Quite a pickle isn't it... A recommendation was to uninstall cgal due to ResolvePackageNotFound, but like I mentioned, I need both cgal and python3.6.
Thanks in advance for the help! (and please include applicable command lines with your suggestions - I'm a beginner)
Looking for Packages when you do a conda create .. or conda install ... , or even a conda search, it will only present you results consistent with your OS and in some cases, base python version.
What's really available You can go directly to Anaconda.org and search for you package. This will surface builds on channels other than the "official" anaconda distribution packages. When looking here, however, you must manually look through the files available as some may not be built for your os.
view the results of conda info to see your current channels. It looks like the channel "conda-forge" may have what you are craving.
conda create -n tamtams_project ipython cgal --channel conda-forge
I prefer knowing where packages are coming from when I stray from the default channels. you can always see that info by adding to your .condarc file with the command:
conda config --set show_channel_urls True
The above create would then show that nearly all of the packages would be conda-forge packages since the command line channel becomes the preferred channel.
So, a minor preference might be to use:
conda create -n tamtams_project ipython cgal -c defaults -c conda-forge
Then you see most packages being default packages and two coming from conda-forge.
Your installed copy of cgal is compiled for Python 3.5. If you look at https://anaconda.org/conda-forge/cgal/files , there's is a separate package for each Python minor version.
Managing Python — Conda documentation recommends either
installing a new Python to a new environment if you don't want to replace an existing installation, or
update'ing it (together with other packages if needed) if you do want to replace the current one
Of course, you can instead uninstall all version-specific packages, install the new Python, then reinstall the packages - but that's more work.

Import theano gives the AttributeError: module 'theano' has no attribute 'gof'

I have python 3.
I installed "Theano" bleeding edge and "Keras" using
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
and also
pip install --upgrade git+git://github.com/Theano/Theano.git
and
pip install git+git://github.com/fchollet/keras.git
But when I try to import Theano, I receive the following error:
AttributeError: module 'theano' has no attribute 'gof'
I looked for a solution online and reached nothing...
This is the piece of code I receive an error on (the last line produces error):
import sys
import numpy as np
import pandas as pd
from sklearn import preprocessing
from keras.models import Sequential
Since I don't have enough experience with python I'm completely lost and can't figure out what to do...
Any help would be appreciated.
The problem arises from a broken installation of theano and has nothing to do with keras itself.
This error seems to be due to conflicts in the installed version of theano, as also suggested in this answer to a related question.
An easy way that should solve the problem without having to fiddle with the installed version and all that is to use conda as package manager and let it do the dirty work.
If you choose to do this be aware that you should manage all of your python modules with it (even though with the latest versions you can install packages with the pip shipped with anaconda itself).
See the official documentation for how to install Anaconda.
Once anaconda is set up you can install theano using simply conda install theano.
With conda is also often convenient to install the packages needed for some particular application, like Keras in your case, in an environment isolated from the rest of your python installation, for easier maintenance.
Read the relevant docs to see how this would work.
I used conda to install theano and still got the same error. After much trial and error and StackOverflow searches, what worked for me was to first run:
conda install m2w64-toolchain
followed by:
conda install theano
Alternatively you can chain the modules together when you create an environment, for example:
conda create -n myenv python=3.5 m2w64-toolchain theano
Also important to follow #gtnbz2nyt's advice and restart your Python instance.
The problem seems to be with your g++ compiler. Try uninstalling it and running your script again. It'll spit a warning implying a degradation in performance, but it'll work nonetheless.
'Python 3.6.3 |Anaconda custom (32-bit)|
(default, Oct 15 2017, 07:29:16)
[MSC v.1900 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 6.1.0 -- An enhanced Interactive Python.
import theano
WARNING (theano.tensor.blas): Using NumPy C-API based implementation
for BLAS functions.
'
For macOS Catalina:
conda create -n pymc3 python=3.8
conda activate pymc3
pip install pymc3

Python unable to find lxml module

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.

Categories

Resources