Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I do if two modules share the same name?

Tags:

haskell

I have two packages that provide modules with the same name. When I try to load the module I get an error

Ambiguous interface for ....: It was found in multiple packages.

What should I do to resolve this?

To be specific, both the cryptonite package and crypto-api package provide modules with the name Crypto.Random. How can I specify which package I want to load the module from?

like image 776
user668074 Avatar asked Nov 04 '17 12:11

user668074


People also ask

Can two different packages have modules with same name?

It's possible to put several modules into a Package. Packages are a way of structuring Python's module namespace by using "dotted module names". A.B stands for a submodule named B in a package named A. Two different packages like P1 and P2 can both have modules with the same name, let's say A, for example.

How can I import two modules with same name?

To import two classes with the same name, use the as keyword to rename one or both of the imports, e.g. import {Employee as Employee2} from './another-file-2. js'; . The as keyword allows us to change the identifying name of the import.

Can Python have more than one class with the same name in a different package?

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.

What is a module Python?

In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.


2 Answers

If you happen to be using ghc >= 8.2 and cabal-install >= 2.0, another option is renaming the conflicting modules in the mixins section of the cabal file:

  build-depends:       base >=4.10 && <4.11,
                       cryptonite >= 0.24,
                       crypto-api >= 0.13.2
  mixins:
                       cryptonite (Crypto.Random as CryptoniteCrypto.Random),
                       crypto-api (Crypto.Random as CryptoAPICrypto.Random)

You can then import the renamed modules:

module Main where

import CryptoniteCrypto.Random
import CryptoAPICrypto.Random

One thing to take into account when renaming this way is that modules that haven't been explicitly renamed become inaccessible.

In fact, ability to rename modules seems to exist since GHC 7.10, through the -package flag and the reexported-modules cabal section. reexported-modules works at declaration-time (when publishing a package) while mixins works at use-time (when depending on a package).

like image 58
danidiaz Avatar answered Nov 10 '22 09:11

danidiaz


You can use the PackageImports language pragma and explicitly pick the package you mean in your import statement like so:

import "cryptonite" Crypto.Random

Alternatively if you have both installed but are only using one of them, you could explicitly list the dependencies you use in a cabal file and build via cabal.

like image 40
gallais Avatar answered Nov 10 '22 08:11

gallais