Install own python package in conda environment - python

I started to write my own python package and want to install this in my virtual conda environment. Now, I'm a little bit confused about the possibilities to do this.
In general, during my search I found these two commands to install my package:
pip install -e <my_package>
conda-develop .
Using the first method leads to the desired result and my package is listed, if conda list has been called (although the package is still not visible in the anaconda navigator, but anyway).
In contrast to this the second method only returned "completed operation for: <path_to_my_package>", but didn't install the packe in my environment.
Does anyone know what this could be or what I am doing wrong? As far as I know, there is also the possibility to create packages directly in conda. Is there any advantage for, if it's only a private package for me?
Thank's a lot in advance.

I think here is the case. When you have a conda environment set up. The packages in the conda will be considered as global package. So, if a package is installed in your conda environment and you choose the conda interpreter in your vent environment, that package will be available. And based on your question, what you want is to be able to install a package that is only available in this vent environment. In this case, you can use terminal to go to your project path. And then use the normal pip install , in this way that package will be in the vent environment only.
Issues may arise when using pip and conda together. When combining
conda and pip, it is best to use an isolated conda environment. Only
after conda has been used to install as many packages as possible
should pip be used to install any remaining software. If modifications
are needed to the environment, it is best to create a new environment
rather than running conda after pip. When appropriate, conda and pip
requirements should be stored in text files.
Use pip only after conda Install as many requirements as possible with
conda then use pip.
Pip should be run with --upgrade-strategy only-if-needed (the
default).
Do not use pip with the --user argument, avoid all users installs.
And here is the official guild about using conda with pip.

Related

how to fix the corrupted base environment

I accidentally install a lot of packages using pip install -r requirements.txt under base environment. Then I tried to pip uninstall, but it seems that the uninstalling process is unsuccessful.
I am using the miniconda on Windows. How can I recover the base environment to clean state? Or do I have to reinstall miniconda to remove the whole base environment?
Unlike Conda, Pip doesn't seem to track revisions and Conda doesn't actively what Pip does (it passively detects installed packages in the lib/python*/site-packages).
One idea around that conundrum might be to export a --from-history YAML:
conda env export -n base --from-history
That will include a section of pip: installed packages and you could use that as the list of packages to remove. It could still be problematic if, for example, Pip installed a newer version of a package from PyPI that is essential for the conda package to function.
If truly broken, you can also restore basic Conda functionality by using a standalone tool like Micromamba. See https://stackoverflow.com/a/75381135/570918, but you'll need to check the docs for Windows installation steps.

How add channel for wolframclient using conda?

I want to use the wolframclient Python package in Mathematica. The docs suggest using pip install wolframclient.
However, my main Python installation is within conda, so I do not want to use pip, but instead a standard conda install wolframclient. (The problem with using pip is that the added package's dependencies may interfere with what conda has already installed and knows about.)
The wolframclient package is at https://pypi.org/project/wolframclient/. But by default conda does not know about pypi.org. I tried the command conda config --add channels pipy, but conda install wolframclient says the channel is "not accessible or is invalid".
I think the problem is that the files at https://pypi.org/project/wolframclient/ are not already in the form of a conda package.
How might one proceed?
I think the problem is that the files at https://pypi.org/project/wolframclient/ are not already in the form of a conda package.
Yes, very precisely noted. Conda packages contain information about how to built in different format than pypi packages, which are made for pip.
In the case you describe, you ahve several options:
Create a seperate environment for wolframclient and use pip install wolframclient in there. Yes, pip and conda do not always get along, but by creating a virtual environment you have no risk of breaking other stuff and pip will be happy installing wolframclient for you
Check the wolframclient package on pypi and install the dependencies using conda before installing wolframclient with pip which might minimize the risk of having to many packages downloaded by pip
Download the source code of wolframclient and built it into a local conda package using conda build and this guide and then use conda install to install the locally built package
I have created a conda build of wolframclient (current version: 1.1.4).

Does a package also gets installed in conda environment if I use pip to install packages not included in Anaconda?

I am currently working on a Django project and want to install an additional Django (app) package called "tinymce". I've installed Anaconda just recently and I'm not used to commands related to conda. I often used to pip install packages from the command prompt. But now I'm really confused if I really should just use the same pip install or use some other conda commands (if there are any) to install that package. Also, does that affect anything, if I am to frequently use that packages?
FYI: I'm on Windows & using Python 3.7 using the recent Anaconda release.
Before you start installing packages, you should decide on how you want to manage your packages for different projects. I'd recommend that you create a dedicated conda environment for each project. Then you have to activate the respective environment whenever you want to work on a project. But packages installed for one project don't interfere with those for another. It helps to install Miniconda rather than Anaconda, because that keeps the conda base environment clean.
You write that you're used to calling pip install, but you don't mention Python virtual envs nor conda environments. That sounds as if you're typically installing packages globally on your machine. Sooner or later, that's going to create a mess.
If you decide to use conda environments, you have to remember to always activate the environment for your project before installing packages for that project. Then both pip install and conda install will put packages into that environment. When I have a choice, I prefer to install packages with conda from its default channels. conda has better dependency management than pip, and conda can handle non-Python dependencies. But packages sometimes have different names in conda and pip, so it might be extra effort to translate installation instructions for pip into similar commands for conda.

In my anaconda's environment tensor, I have two pips . How can I uninstall the old version? my OS is windows10

There are two pips in my environment, I use the command "conda list" to list them
pip 18.0
pip 9.0.1 py35_1
I want to remove pip 9.0.1, how can I do it?
Have you tried conda uninstall pip=*version*?
You could also run conda uninstall pip (possibly conda uninstall pip*) and remove both versions, then install the latest version of pip after that.
Two versions of pip in a single conda env should not happen through the usual update processes. Either you mistakenly installed the second, or something went very wrong with anaconda. If you suspect that it is something on anaconda's end, then this is a good opportunity for some spring cleaning. Export and then remove the environment, update and clean conda, then either:
Rebuild the environment by removing anything you don't need from your exported environment file and importing this environment again. Then run another conda update --all to ensure you are on the latest version for these packages.
OR Create a fresh environment, manually go through your exported environment and install only your most-used packages. I'd recommend this one, to get a better understanding of what dependencies your projects actually need.
Addendum:
Kota Mori comments that:
Two pip's can happen if you: 1. conda istall pip, then 2. pip install pip --upgrade.
I did not consider this in my original answer. The Conda user guide does not explicitly advise against installing packages through a non-conda version of pip: Anything installed by your pip 18.0, is in the currently active conda environment.
Pip packages do not have all the features of conda packages, and we recommend first trying to install any package with conda. [...] To gain the benefits of conda integration, be sure to install pip inside the currently active conda environment, and then install packages with that instance of pip.
However, (and this is speculation) given that Anaconda aims to simplify package management for Python and R, it may be developed for the pip version delivered by conda. Delivering v10 would then be deliberate. I would personally be wary of unintended side effects and would choose to recreate my environment if I cannot cleanly uninstall v18 and return to v10, or whichever version is currently distributed through conda.

same package installed by both pip and conda

What happens if the same package is installed by both pip and conda in the same environment? Is conda designed to cope with this? Can you safely pip uninstall the pip version without messing up the conda version?
They will be installed in the same directory such as /home/user/miniconda/env/envname/lib/python3.6/site-packages/requests.
So if you install a package by both conda and pip, then uninstall it by pip, the source code has gone. And that means you cannot use this package any more.
When installing packages, pip will check dist-info or egg-info directory while conda will check conda-meta directory. In this case, you can install the same package both by conda and pip if you install it by pip first and then install it by conda. In the reverse case, pip will consider that package has been already installed.
To completely uninstall a package installed both by conda and pip, you need to run both conda remove to remove information in conda-meta and pip uninstall to remove dist-info directory.
According to this post on the Anaconda website, it depends on the package installed.
Issues can arise when conda and pip are used together to create an environment, especially when the tools are used back-to-back multiple times, establishing a state that can be hard to reproduce. Most of these issues stem from that fact that conda, like other package managers, has limited abilities to control packages it did not install. Running conda after pip has the potential to overwrite and potentially break packages installed via pip. Similarly, pip may upgrade or remove a package which a conda-installed package requires. In some cases these breakages are cosmetic, where a few files are present that should have been removed, but in other cases the environment may evolve into an unusable state.
You can remove the package installed as shown in the first answer. But an environment can be restored to a previous revision, in order to undo the damages done, if there are any, by using conda and pip together.
To list the history of each change to the current environment, use conda list --revisions
And to restore it to the previous version, use conda install --revision 2, where 2 is a selected revision number.

Categories

Resources