Importing a package installed with anaconda in virtual environment - python

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.

Related

Issue with installing python modules

I am pretty new to python. Just been working through some online tutorials on udemy. I seem to have an issue with pip installing modules.
I've tried reinstalling them.
Upgrading my python version.
In VS I always just get module not found.
If I do it in the cmd prompt this is what I get below.
You are currently working on the base environment of your computer. For safety, you can first create a new virtual environment with
python3 -m venv -n new_env
So that you won't corrupt any default installations. Then, activate it with
source new_env/bin/activate
And update the pip and setuptools with
pip3 install --upgrade pip
pip3 install --upgrade setuptools
Finally, install numpy via
pip3 install numpy
However, I would recommend using Anaconda to build your virtual environment. When you install Anaconda and make sure it is included in the path of your terminal, all you need to type is
conda create -n new_env python=3.7 numpy
and it will automatically build the wheel for numpy. Here, "new_env" is just an example for a virtual environment name, and Python version 3.7 is also an example.
You can then, activate this conda environment by
conda activate new_env
To use this virtual environment, which you built either with "venv" or "conda", you should locate and activate this environment from the project interpreter settings in VS .
Finally, I would also recommend considering Pycharm IDE which can also help you with creating a virtual environment and installing packages in it.
It seems that you already have the packages installed. Using VS, please, be sure that you selected the correct Python interpreter (https://code.visualstudio.com/docs/python/environments)

Install packages with Conda for a second Python installation

I recently installed Anaconda in my Windows. I did that to use some packages from some specific channels required by an application that is using Python 3.5 as its scripting language.
I adjusted my PATH variable to use Conda, pointing to the Python environment of the particular program, but now I would like to use Conda as well for a different Python installation that I have on my Windows.
When installing Anaconda then it isn't asking for a Python version to be related to. So, how can I use Conda to install into the other Python installation. Both Python installations are 'physical' installations - not virtual in any way.
Uninstall the other python installation and create different conda environments, that is what conda is great at.
Using conda from your anaconda installation to manage packages from another, independent python installation is not possible and not very feasible.
Something like this could serve your needs:
Create one env for python 3.5 conda create -n py35 python=3.5
Create one env for some other python version you would like to use, e.g. 3.6: conda create -n py36 python=3.6
Use conda activate py35, conda deactivate, conda activate py36 to switch between your virtual environments.

How to install the specific version of Python with Anaconda?

I want to install Anaconda with Python Version 3.6.5. If I install Anaconda3-5.2.0, It install Python 3.5.1. Where to download the Anaconda with Python 3.6.5. The Big Data Scripts work only with Anaconda Python 3.6.5.
Anaconda Downloads
The Anaconda distribution with Python 3.6.5 was version 5.2.0.1 You can download this from the Anaconda distribution archive. If you do install from this, then make sure to update Conda immediately after installation:
conda update conda
However, I strongly recommend the following alternate solution as better practice.
Miniconda + Anaconda environment
Reasoning
What is installed in the base environment is relatively fixed once installed. Ultimately, you don't want to mess with your base environment, so best practice is to have the latest version there. Fortunately, you don't have to install a full Anaconda distribution, but rather can use a lightweight Miniconda (or Miniforge) distribution and create a secondary environment for the purpose of having an Anaconda Python 3.6.5 distribution. In the long run this will give you better stability.
Steps
Download and install Miniconda or a Miniforge variant. Once that is working...
Create your Anaconda env:
conda create --name my_env -c anaconda python=3.6.5 anaconda=5.2.0
Use your new isolated env:
conda activate my_env
[1] I determined this by running conda create -n foo --dry-run -c anaconda python=3.6.5 anaconda and then examining the version of the anaconda package that Conda ended up with in the solve.
Also try
conda install python=3.6.5
but you may encounter some incompatibility issues with other packages.
Alternatively, you may want to try creating a new environment. From the anaconda prompt, create a custom environment and specify the repository channel to find the version
conda create --name py365 python=3.6.5 --channel conda-forge
Activate the new environment
conda activate py365
However, the activation will not be permanent, and you will need to activate each time you start the anaconda prompt.
In your anaconda prompt, you can manually update your python to the latest version with :
conda update python
In case you are not familiar with it, anaconda prompt is installed to your computer when you install anaconda. Just make a search for it on your computer.
You can refer to this post : How do I upgrade to Python 3.6 with conda?

Do these commands do the same action?

Basically I would like to know if those 2 snippets do the same thing :
conda install -n myEnv myPackage
VS
conda activate myEnv
pip install myPackage
Or in a different way, does a pip install when a conda environment is activated equal doing a conda install on myEnv ?
EDIT : I thought it was obvious but => more precisely, does the second snippet only install the package on the environment or on the overall system ?
PS : Asking because there's a package available with pip but not with conda and I want it to only be installed on myEnv
The Anaconda docs make it clear that if you use conda as your virtual environment manager, you should stick to conda install to install new packages as far as possible:
Unfortunately, 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. … 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.
If you can't get all the packages you need from a conda channel, they say this, which is good advice even if you don't use pip:
If there is an expectation to install software using pip along-side
conda packages it is a good practice to do this installation into a
purpose-built conda environment to protect other environments from any
modifications that pip might make.
Finally the same document notes:
Use conda environments for isolation
create a conda environment to isolate any changes pip makes
environments take up little space thanks to hard links
care should be taken to avoid running pip in the “root” environment
Provided you activate the correct conda environment first, the pip install command(s) should use that environment's pip and install only into that environment.
Yes and no.
pip downloads and installs the package from PyPI whereas conda does the same from Anaconda repositories.
There are packages in PyPI not present in Anaconda, and the other way around.
For managing the environment I would choose one way or the other, since with pip you can freeze into a requirements.txt (pip freeze > requirements.txt) and conda you can either export the whole environment (conda env export) or the list of packages (conda list --export > requirements.txt). However if you try to use a conda-generated file from pip, it will most probably fail.

Scipy & Ipython Notebook: Package mangement tool?

I have a Ipython Notebook that I'd like to share with others, and it uses a lot of packages.
I'm wondering if there is any tool for installing packages with ease? So others won't need to run pip install for each packages that I listed.
In Ruby on Rails, there is a gemfile, I can just run bundle install and then all gem are installed, which saves a lot of time.
I'm wondering if there is a gemfile and bundle install for ipython notebook? So we can install packages with ease.
One way to do it is to use pip:
pip freeze > requirements.txt
You could distribute that with the notebook. Then to use it:
pip install -r requirements.txt
Here is the how to with requirements.txt method. But the basic workflow is above.
As a note the requirements file will look something like:
requests==2.8.1
SQLAlchemy==0.9.9
stripe==1.27.1
Werkzeug==0.10.4
wheel==0.26.0
WTForms==1.0.5
Where you can see there are versions for each package. This method handles dependencies as well. So if one package depends on another, pip installs in such a way that there shouldn't be any errors. Though it might not always be the case.
This method should work in a Windows Powershell, definitely does in Mac and Linux.
Another is with conda:
conda create -n myenv python==3.5.0
This creates a conda environment. Which can be activated or deactivated. If activated you can install from a dependency file as:
conda env create -f requirements.yml
Similarly the requirements.txt can be created as:
conda env export > requirements.yml
For scientific applications conda is probably the best option. It allows to install from a file of package names:
conda install --file file_with_package_names.txt
Furthermore, it offers virtual environments that are more powerful than the standard virtual env:
conda create -n my_new_env python=3.5
Activate this environment:
source conda activate my_new_env
Get help with:
conda -h
List all installed packages:
conda list
List all conda environments:
conda info -e
It offers much more and works on all major operating systems. All installs are binary. So no compilation of extensions. Makes Windows users very happy. But is great for Linux/Mac folks too.
This is a nice comparison of conda, pip, and virtualenv.

Categories