How to clean local python environment of conda installs - python

Using the latest miniconda2 I created a conda environment & then did all my conda package installs. Then when I finished I realized I had FORGOT TO ACTIVATE the environment (I'm dizzy) with the command
source activate myenv
so all my conda installs went into the local/system wide environment. I simply did
conda uninstall package-name
for all the packages but I'm certain not all the dependencies were removed. For example, conda install of matplotlib showed
The following NEW packages will be INSTALLED:
cycler: 0.10.0-py36_0
dbus: 1.10.20-0
expat: 2.1.0-0
fontconfig: 2.12.1-3
freetype: 2.5.5-2
glib: 2.50.2-1
gst-plugins-base: 1.8.0-0
gstreamer: 1.8.0-0
icu: 54.1-0
jpeg: 9b-0
libffi: 3.2.1-1
libgcc: 5.2.0-0
libiconv: 1.14-0
libpng: 1.6.30-1
libxcb: 1.12-1
libxml2: 2.9.4-0
matplotlib: 2.0.2-np113py36_0
pcre: 8.39-1
pyparsing: 2.2.0-py36_0
pyqt: 5.6.0-py36_2
qt: 5.6.2-5
sip: 4.18-py36_0
which conda shows
/home/cargo/miniconda2/bin/conda
First I created the environment
conda create -n myenv python=3
Then I did all the conda installs
conda install numpy
conda install pandas
.
.
conda install statsmodels
Then I realized I failed to activate the environment b/c the terminal prompt was
cargo#cargo-VirtualBox:~$ INSTEAD OF (myenv)cargo#cargo-VirtualBox:~$
The terminal prompt is what got my attention before I even tried to do anything else; I'm 110% certain I didn't activate the environment as I can't find the command in the bash history. Next I did uninstalls for all the conda packages as stated above. I guess it's good now that I no longer see the python3 packages in the local/root environment but I can't explain/understand why?
conda list
# packages in environment at /home/cargo/miniconda2:
#
asn1crypto 0.22.0 py27_0
beautifulsoup4 4.6.0 <pip>
cairo 1.14.8 0
cffi 1.10.0 py27_0
.
.
wheel 0.29.0 py27_0
yaml 0.1.6 0
zlib 1.2.8 3
cargo#cargo-VirtualBox:~$

Related

Installing GeoPandas on Spyder 5.1.5

Since updating Spyder to version 5.1.5, I'm unable to install GeoPandas. I've tried the following:
conda install geopandas
conda install --channel conda-forge geopandas
conda install -c conda-forge geopandas
and
conda create -n geo_env
conda activate geo_env
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
conda install python=3 geopandas
and none seem to work. I've also installed all the necessary dependencies, with the exception of Fiona (which returns the same installation problem).
After some research and a helpful post, I figured out a solution. There must have been an error when I previously updated Spyder to 5.1.5, and even though I had uninstalled and reinstalled Anaconda a number of times, GeoPandas wouldn't install until I removed Spyder through the Anaconda Prompt.
conda remove spyder
conda remove python-language-server (may return error but not a problem)
conda update anaconda (may return error but not a problem)
conda install spyder=5.1.5
conda install pandas
conda install geopandas
conda install openpyxl (had to reinstall)

How to update flask from version 1.1.2 to version 2.0 on anaconda?

When I install the latest version of anaconda distribution, it comes with flask version 1.1.2. However, I need to install flask version 2.0. How can I install flask version 2.0 (in the base environment) while keeping other existing packages installed such as sklearn and numpy, etc.?
First do conda activate base,
Then you can either do pip install flask==2.0.0 or conda install -c conda-forge flask==2.0.0. Both are okay as long as you do conda activate base first.
You should consider creating a new environment for your project and not mess with the base environment.
First do conda create -n newname, then conda activate newname.
Then you can do conda install -c conda-forge numpy pandas sklearn flask==2.0.0, you can also do pip install numpy pandas sklearn flask==2.0.0. You can install everything you need in one line.
In a nutshell:
conda activate base
conda remove flask
conda install -c conda-forge flask
Line 1: to activate your base virtual environment
Line 2: in order to avoid any conflict, remove flask 1.1.2
line 3: install the latest flask version via the channel conda-forge (2.0.1 as for now)
If you want the 2.0.0 you should do this: conda install -c conda-forge flask=2.0.0
Explanations:
Channels:
conda uses channels (like repositories where the packages are stored). By default, the channel is the anaconda channel. So, if you installed flask with conda install flask that is the same as if you did conda install -c anaconda flask. And from this page (https://anaconda.org/anaconda/flask) you can see that the actual version on this channel is 1.1.2.
About the update part:
In general, to update all your packages in a virtual environment, you first have to activate your virtual environment with conda activate [virtual_environment_name] and then conda update --all.
But as you can see from the previous link, on the default channel (anconda), the latest version is 1.1.2, so, strictly speaking, you can't update flask to a version greater than 1.1.2.
But, because on the conda-forge channel the latest version is -as for now- 2.0.1, if you install flask from this channel, you will get flask 2.0.1.
Using pip in a Conda environment:
An other option mentionned by #anarchy too is to use pip install flask=2.0.0 but you should avoid that. See: https://www.anaconda.com/blog/using-pip-in-a-conda-environment and
Is that a bad idea to use conda and pip install on the same environment?.
About not using the base environment directly:
And in order to have a clean workspace, it's a good practice to create a virtual environment other than the base environment for your projects. You can create and then activate a new environment named for example "myenv" like this:
conda create -n myenv
conda activate myenv
And so the complete code would be:
conda create -n myenv
conda activate myenv
conda install -c conda-forge flask
And finally if you want to install the latest versions of other packages like numpy and sklearn, you should use conda-forge too:
conda install -c conda-forge scikit-learn
conda install -c conda-forge numpy

How to make sure python version is running correctly

I'm relatively new to Jupyter Notebook and have been struggling with python versions with Jupyter Notebook.
I installed seaborn but import error occurred saying no seaborn package found. It shows on upper right corner of Jupyter "Python 3" but it returned Python 2.7 when I run !python --version. Also when I run print(sys.path), the result is below.
['', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mysql-0.0.1-py3.5.egg', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions', '/Users/Cynthia/.ipython']
My guess is that my python kernel isn't pointing correctly to python3 although notebook shows it's python3. Could someone pls help me solve this? It would be helpful if there could be code to run in Jupyter cell. Thank you!
When you run python --version, It won't spit python 3.5.x, because python refers to python2 unless you aliased python as python3. So it makes sense that you see python 2.7.x when you run python --version.
As for py2 when you run conda env list, they are env names you set. They are just names, not python versions.
What needs to be done I think is to find out where your jupyterlab is installed, which I think in (base) environment. In your base environment, run conda list, where you will see a list like this:
(base) ➜ test conda list
# packages in environment at /Users/gwanghyeongim/.pyenv/versions/miniconda3-latest:
#
# Name Version Build Channel
brotlipy 0.7.0 py38haf1e3a3_1000
ca-certificates 2020.6.24 0
certifi 2020.6.20 py38_0
cffi 1.14.1 py38hed5b41f_0
chardet 3.0.4 py38_1003
conda 4.8.4 py38_0
conda-package-handling 1.6.1 py38h1de35cc_0
cryptography 2.9.2 py38ha12b0ac_0
idna 2.10 py_0
libcxx 10.0.0 1
libedit 3.1.20191231 h1de35cc_1
libffi 3.3 hb1e8313_2
ncurses 6.2 h0a44026_1
openssl 1.1.1g h1de35cc_0
pip 20.2.2 py38_0
pycosat 0.6.3 py38h1de35cc_1
pycparser 2.20 py_2
pyopenssl 19.1.0 py_1
pysocks 1.7.1 py38_1
python 3.8.3 h26836e1_1
python.app 2 py38_10
readline 8.0 h1de35cc_0
requests 2.24.0 py_0
#and so on...
See if you see jupyterlab in the list. If so, your jupyter notebook is in (base) environment.
Now the most likely scenario is you installed seaborn in py2 environment. That means you dind't install seaborn in your base environment. Install it by running conda install seaborn or pip install seaborn.
If something didn't work so far, try runnning conda upgrade --all -y to upgrade packages. It might be from collision between deprecated packages.
P.S
My suggestion is you create a separate environment and run packages on it.
Run conda create -n your_env_name to do so(replace your_env_name to the name you want set)
Activate by running conda activate the_env_you_just_created
If 2 doesn't work somehow, make sure you run conda init your_shell, where your_shell can be found by running echo $SHELL, where the last word after / is your shell.
Make sure you see (your_env_name) at the first part of command prompt. If so, your env is activated. Now install packages on here and do your project, rather than on base environment.
Since you mentioned you use conda you can do something like the following. From your terminal:
conda create -n sb python=3
conda activate sb
conda config --env --add channels conda-forge
conda install -y pandas matplotlib numpy scipy seaborn jupyterlab # some default packages
jupyter lab
Whenever you want to use this conda environment again you have to do
conda activate sb
before you can run jupyter lab.
Note, if you didn't changed the default, you should see your terminal prompt changing when activating an environment, i.e. the name of the environment comes before your prompt. In our case here (sb) <prompt>.
To solve the issue with your current conda environment, more information is needed.

Capture snapshot of current python environment and recreate on another machine

I have an environment created using miniconda with python 3.6.8, called basepy_3_6_8.
I want to save the environment snapshot to a file and then recreate it later on another machine:
There are different commands to capture the environment snapshot, with slightly different outputs. Which of these can I use to guarantee that the exact environment used by the user is recreated in the target?
I was hoping pip freeze > requirements.txt and pip install -r requirements.txt would work independent of the source environment, but I noticed that pip freeze from within a conda environment does not capture the python version.
Here is the code to create the conda environment, and output of different commands:
$ conda create -n myenv python=3.6.8
$ conda activate myenv
(myenv)$ pip freeze
astroid==2.1.0
autopep8==1.4.3
certifi==2018.11.29
colorama==0.4.1
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
pycodestyle==2.4.0
pylint==2.2.2
six==1.12.0
typed-ast==1.1.1
wincertstore==0.2
wrapt==1.11.0
(myenv)$ pip list
Package Version
----------------- ----------
astroid 2.1.0
autopep8 1.4.3
certifi 2018.11.29
colorama 0.4.1
isort 4.3.4
lazy-object-proxy 1.3.1
mccabe 0.6.1
pip 18.1
pycodestyle 2.4.0
pylint 2.2.2
setuptools 40.6.3
six 1.12.0
typed-ast 1.1.1
wheel 0.32.3
wincertstore 0.2
wrapt 1.11.0
(myenv)$ conda list
# packages in environment at C:\Users\alias\AppData\Local\Continuum\miniconda3\envs\myenv:
#
# Name Version Build Channel
certifi 2018.11.29 py36_0
pip 18.1 py36_0
python 3.6.8 h9f7ef89_0
setuptools 40.6.3 py36_0
sqlite 3.26.0 he774522_0
vc 14.1 h0510ff6_4
vs2015_runtime 14.15.26706 h3a45250_0
wheel 0.32.3 py36_0
wincertstore 0.2 py36h7fe50ca_0
(myenv)$ conda list --export
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
certifi=2018.11.29=py36_0
pip=18.1=py36_0
python=3.6.8=h9f7ef89_0
setuptools=40.6.3=py36_0
sqlite=3.26.0=he774522_0
vc=14.1=h0510ff6_4
vs2015_runtime=14.15.26706=h3a45250_0
wheel=0.32.3=py36_0
wincertstore=0.2=py36h7fe50ca_0
I am eventually interested in a general tool that can capture the current environment of a specified type (conda, virtualenv, venv, global python environment) so as to install it uniformly on another machine. What is the best approach for this?
I've never used conda, but I'd try to use two different tools to manage the python version and your project dependencies.
To install a specific python version, I'd use pyenv: https://github.com/pyenv/pyenv.
pyenv also has a plugin to manage virtualenvs (https://github.com/pyenv/pyenv-virtualenv) that should support Anaconda and Miniconda: https://github.com/pyenv/pyenv-virtualenv#anaconda-and-miniconda
To manage your dependencies (packages you install in your virtual env), you have a few alternatives:
Pip freeze: it doesn't automatically guarantee reproducibility though, because it doesn't have a lock file to pinpoint the exact dependency tree
Poetry: https://github.com/sdispater/poetry (supports a lock file)
Pipenv: https://github.com/pypa/pipenv (supports a lock file)
Hope this is helpful.

Anaconda python: Pinning channel and not package version

Every time I use conda install -c some_channel to install a Python package, conda tries to update other packages from some_channel. From example, here I'm trying to install hdbscan from conda-forge, and conda wants to update ca-certificates as well.
The following NEW packages will be INSTALLED:
hdbscan: 0.8.18-py36h7eb728f_0 conda-forge
The following packages will be UPDATED:
ca-certificates: 2018.03.07-0 anaconda --> 2018.8.24-ha4d7672_0 conda-forge
certifi: 2018.8.24-py36_1 anaconda --> 2018.8.24-py36_1001 conda-forge
openssl: 1.0.2p-h1de35cc_0 anaconda --> 1.0.2p-h470a237_1 conda-forge
Proceed ([y]/n)? n
Note that conda tries to do this even if the package versions from the two channels are identical. So this is not just about upgrading to newer version.
Can I pin the channel for packages without pinning the version numbers? I wouldn't mind updating ca-certificates if the update comes from anaconda channel.

Categories