简体   繁体   中英

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?

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'm not sure how well anaconda and virtual environments ie 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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM