Sometimes you want to contain effects of some operation, for example you want to import a key to inspect it, but you do not want this key to be added to your keyring. In earlier versions of GnuPG, it was possible to specify alternate keyring files for both public and secret keys. In modern GnuPG versions, however, we changed how secret keys are stored in order to better protect secret key material, and it was not possible to preserve this interface.
The preferred way to do this is to use ephemeral home directories. This technique works across all versions of GnuPG.
Create a temporary directory, create (or copy) a configuration that
meets your needs, make
gpg use this directory either
using the environment variable GNUPGHOME, or the option
--homedir. GPGME supports this too on a per-context basis,
by modifying the engine info of contexts. Now execute whatever
operation you like, import and export key material as necessary. Once
finished, you can delete the directory. All GnuPG backend services
that were started will detect this and shut down.