Running pipenv with specific conda env? - python

I have a conda environment, cenv. While activated, I've installed pipenv via:
(cenv) % conda install -c conda-forge pipenv
I created a pipenv env in a repo by installing a package I'll need, in this case the GDAL bindings, gdal:
(cenv) % pipenv install gdal
All good so far. Now when I want to launch the pipenv shell to see if my installation worked in python, I run:
(cenv) % pipenv shell
And I get:
Launching subshell in virtual environment…
. /home/<user>/.venvs/cenv-<scramble>/bin/activate
(base) % . /home/<user>/.venvs/cenv-<scramble>/bin/activate
(modis_lst) (base) %
So I get kicked out of the (cenv) conda env and put in the system conda env, (base), which is not set up how I need. I can deactivate the (base) env, and activate the (cenv) env, but I'm looking for a better solution. Because I'm not sure I could use pipenv run in this way.
How can I start a pipenv shell or run pipenv while choosing the conda env I want to use? Thanks :)

Looks like this related question/answer solves my problem. Needed to run conda config --set auto_activate_base false to disable - you guessed it - auto-activation of the (base) env. Now I can run pipenv shell/pipenv run and the pipenv env will launch within the currently activated conda env.

Do you need to create new virtual env in conda using pipenv?
If it true, you no need to use pipenv. In anaconda navigator, choose Environment tab, create a new one. Done.
If not, please explain, I want to know a new thing.

Related

No pip or conda command found in my virtual env

I was working on project and had setup a virtualenv. Everything was working fine until severe got crashed. When the server rebooted, I see all my installed packages in virtualenv is lost. When I try to install packages using "pip" I see "pip : command not found" error. Later, I found that I have pip command is working outside of virtual env but not inside the virtual env. I am not sure how to solve the pip issue. I have few questions which are as follows:
Do I need to set the path of pip inside virtual env to make it work? if yes, how to set it up?
When I check my virtual env folder inside my repository I see the pip, pip3.... that means the virtual env has pip command but show how it is not able to call it.
Should I delete my virtualenv and create new virtualenv? If yes, how I may affect my existing code.
Or is there any other way. Any help would be appreciated. Thanks
# put conda on PATH like
. '/SOMEWHERE/conda/etc/profile.d/conda.sh'
# then
conda activate
# or look under conda\envs
conda env list
# if you were just using base I'd recommend a create
conda create -n environment_name
# yes then reinstall everything
conda activate environment_name
I like putting conda activate environment_name in an rc/profile depending on your terminal type as to where

How to still using anaconda Python3 after activate a conda environment?

I'm using MAC OS and installed python2, then miniconda python2, and then Anaconda python3.
Now normally in terminal my python version would be python3, but if I activate a conda environment "test", then the default python will be python2.
I believe the reason is the $PATH is changed, at "base" env (when terminal started), the starting part is "/anaconda3/bin:/anaconda3/condabin", and after "conda activate env", it changed to "/anaconda3/envs/test/bin:/anaconda3/condabin" which do not have python3 in those bin folder.
Is there a method to make the conda environment using the python3 of Anaconda?
I tried to create a link (both symbolic and hard) /anaconda3/envs/test/bin/python -> /anaconda3/bin/python. By this way, anaconda python3 can be started inside conda env. Then I found pip still not reachable. I think may create a pip link file too. But I guess this is not the correct way to use a conda environment?
(base) $which python
/anaconda3/bin/python
(base) $which pip
/anaconda3/bin/pip
(base) $conda activate test
(test) $which python
/usr/bin/python
(test) $which pip
'''no result'''
I expect the created conda env to use the same python3 as in base env, and can use pip, without setup symbolic or hard link.
When you activate a conda environment, you'll use the Python version from that environment. pip will automatically install into the active conda environment, unless you do something to break it, for example by calling another pip installation than the one from the current environment.
If you want to use a different Python, then don't activate the conda environment. If you want to use a specific version of Python in an environment, then install that version into that environment. Inside an environment, you can only (reasonably) use the Python version that's installed in the environment.
By the way, it is pointless to install different versions of Anaconda or Miniconda alongside. Just install Miniconda and create conda environments for everything else. Install the package anaconda into an environment to get the packages that Anaconda brings along.
I think you have added the path of "/usr/bin/python" in $PATH by anyway. So while doing python from inside a specific conda environment you are redirected to use the "/usr/bin/python" instead of "/anaconda3/bin/python". You can check the path by
$ $PATH

Anaconda is hanging in Solving Environment

I tried to install py2exe using conda install -c kieranharding py2exe and pyinstaller but it hangs in solving environment.
I changed conda config --add channels conda-forge and conda config --set channel_priority strict and flexible.
But just keeps hanging. I managed to install in another environment but I want py2exe to install to root env.
That version is only available for Python 3.4. If you're trying to install this in base env, then that command is going to send Conda into trying to figure out how to reconfigure your entire installation to use 3.4, which is a very heavy task. Instead, create a new env and that shouldn't have the same trouble:
conda create -n -c kieranharding python=3.4 py2exe
see my another answer
conda update -n base conda
conda create --name myenv
conda activate myenv
try it

Importing a package installed with anaconda in virtual environment

I want to work with the python package holopy. Apparently you have to use conda to install it, so I first installed Anaconda 4.2.0 (since I'm using Python 3.5). I opened the virtual environment I normally use and installed holopy as they recommend on the official site:
conda install -c conda-forge holopy
Afterwards, when in the virtual environment I type conda list, holopy shows up. But when I type python3 and then import holopy, it says package not found. It does however work when I leave the virtual environment. I need it in the virtual environment though, how can I do that?
I'm not sure how well anaconda and virtual environments i.e.venv work together. If you're using anaconda anyway then I highly recommend using anaconda environments. Please go through this short tutorial about anaconda environments - you won't regret it.
Why it didn't work for you?
The conda command is available only in the base anaconda environment. So when you run the command - conda insall -c conda-forge holopy, it installed holopy in the base anaconda environment and it won't be available to you in your venv.
After looking at the documentation of holopy it seems probable that when they said virtual environment they actually meant anaconda virtual environment. Therefore the solution is to first create an anaconda virtual environment called holopy-env and then run the command conda install -n holopy-env -c conda-forge holopy.
A better way of doing things with Anaconda
I will also give you a quick and clean example of how to create an environment using anaconda. If you're using Anaconda then it would be wise to use it's environment management tools. Create an environment.yml file with the following contents:
environment.yml using conda-forge/holopy & python 3.6
name: holopy-env # any name for the environment
channels:
- conda-forge
dependencies: # everything under this, installed by conda
- python=3.6
- holopy
- pip: # everything under this, installed by pip
- future
How to install the environment?
conda create --force -f environment.yml
How to activate the environment?
source activate opencv-env
After activating the environment
You should be able to import holopy
Install pip packages using pip install <package>
Install conda packages using conda install -n holopy-env -c CHANNEL <package>
conda is a packaging tool and installer that aims to do more than what pip can do; handle library dependencies outside of the Python packages as well as the Python packages themselves. Conda also creates a virtual environment, like virtualenv does. For creating virtualenv with conda, use the following command:-
conda create -n yourenvname python=x.x anaconda
Use the following to activate the virtualenv in conda
source activate yourenvname
Then, you can install the packages in virtualenv using conda as:-
conda install -n yourenvname [package]
To Deactivate use:-
source deactivate
And to delete a no longer needed virtualenv, use :-
conda remove -n yourenvname -all
I know this is a bit late, but you don't need to use conda to install HoloPy. This is just the least technical option. Otherwise, you need to be able to compile HoloPy's fortran components yourself, which is fairly straightforward on Unix-based systems but complicated on Windows. Instructions can be found in HoloPy's documentation at https://holopy.readthedocs.io/en/latest/users/dev_tutorial.html.
We are also working on putting together a singularity container distribution of HoloPy. Let me know if this is of interest to you and I will make it a priority.

How to install regular python (via homebrew) and miniconda in the same computer?

I downloaded conda, however I would like to use pip and a regular python version (homebrew) for a different purpose, is it ok if I install python and pip via brew and then I install conda?
Update
after installing miniconda I tried to install python via homebrew and both python versions crashed. How can I install miniconda and then python via homebrew?
Anaconda:
Conda creates language-agnostic environments natively whereas pip
relies on virtualenv to manage only Python environments Though it is
recommended to always use conda packages, conda also includes pip,
so you don’t have to choose between the two. For example, to install a
python package that does not have a conda package, but is available
through pip.
You can also use pip within your conda environment:
conda install pip
pip <pip command>
or
conda install -n testenv pip
source activate testenv
pip <pip command>
You can also add pip to default packages of any environment so it is present each time so you don't have to follow the above snippet.
From Anaconda Troubleshoot FAQ, following methods can be employed :
Edit your .bash_profile and .bashrc files so that the conda binary directory, such as ~/miniconda3/bin, is no longer added to the PATH environment variable. You can still run conda activate and deactivate by using their full path names, such as ~/miniconda3/bin/conda.
You may also create a folder with symbolic links to conda, activate and deactivate, and then edit your .bash_profile or .bashrc file to add this folder to your PATH. If you do this, running python will invoke the system Python, but running conda commands, source activate MyEnv, source activate root, or source deactivate will work normally.
After running source activate to activate any environment, including after running source activate root, running python will invoke the Python in the active conda environment.
As of 2019, Amit Singh's comment is slightly out of date (and the link is broken). I had the same issue previously, but needed to install conda to work on a specific project with a collaborator. I did this using miniconda, but it should work with Anaconda as well.
After installing conda, this is added to ~/.bash_profile (or .bashrc):
added by Miniconda3 4.5.12 installer
>>> conda init >>>
!! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/Users/<user>/miniconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/Users/<user>/miniconda3/etc/profile.d/conda.sh" ]; then
. "/Users/<user>/miniconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/Users/<user>/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
<<< conda init <<<
It basically automatically activates a base conda environment and puts you in it when you start a new shell. This adds conda's python, and other software, to your path. Any tools looking for system/homebrew install programs such as python will get the conda versions instead if they exist. This can create problems if you are trying to use the homebrew versions of things.
This whole block can be safely deleted. Instead, add:
. /Users/<user>/miniconda3/etc/profile.d/conda.sh
to you ~./bash_profile. This calls a script which creates bash functions for conda, conda activate, and conda deactivate and sets some environment variables. Importantly, it doesn't active the base environment (the default, global conda environment) or change your path.
You can now create a conda environment for your project and install whatever you need into that:
conda create -n my_project python R jupyter # Whatever packages you need
conda activate my_project
# do some stuff
conda deactivate # leave the environment
# do unrelated stuff without issues
If you do want to use the default (root) environment, it can be activated like any other:
conda activate root
# do some stuff
conda deactivate
I hope that helps!

Categories