Install Anaconda in a specific location - python

I'm wondering how to go about installing Anaconda miniconda in a particular location on my system.
If I have some project
~/some-project
That I want to use conda for, but don't want to use conda anywhere else on my system, how do I go about that?
Ideally there will be some approach where I can do
cd ~/some-project
install miniconda here only as some-project-env
And I can then, within ~/some-project, activate some-project-env. This solution will not touch any rc configuration files, all configuration settings for some-project-env will be contained within ~/some-project.
So, if I was to only create some-project-env, and then deleted ~/some-project, there would be no trace of conda on my system.
Edit
It would still be of interest to have a solution to this post. Something that might be of use to someone finding this is the following in their ~/.condarc
auto_activate_base: false

Related

Creating an offline package folder for Anaconda

I have updated the anaconda packages for the python program I was working on. I now want to install these new packages on computers that are offline.
I have generated a list of the installed packages using the following command:
>conda list -e > packagelist.txt
I want to be able to update the packages on the offline computers using something like this:
>conda install --file (location of 'packagelist') --channel file://(location of the folder where the packages are located)
So my question is: how do I easily make a folder, "pkgs", that contains all the package files indicated in my package list?
Can I simply just use the entire "pkgs" folder that is located in my current Anaconda installation location ? Or are the packages in my package list not necessarily installed inside there?
I apologize if the question is 'nooby', I honestly don't know how to. Looking forward to any help.
If anyone else is struggling with this, these are the steps I took:
Create a file containing the names of all the packages installed in the current environment. One way to do this(already shown in question) is by typing the following on a command prompt (run as admin):
conda list -e > packagelist.txt
Go to the "pkgs" folder that is located in your Anaconda installation directory. Copy all the zipped packages there (only the ones that have a ".tar.bz2" file extension), and paste them inside another folder somewhere, lets call it "pkgs2".
Compare the packages in the "packagelist" text file you created with the packages inside the the new folder you created, "pkgs2". Delete older package versions.For example, there might be two different numpy versions in your "pkgs2" folder. Delete the outdated one if necessarily.
Create index and repodata files. I'm not entirely sure why. The important one is the "repodata.json.bz2" file. You can create this by typing the following in command prompt (obviously your "pkgs2" might be located somewhere else, so adjust accordingly):
conda index C:\Users\myName\Desktop\pkgs2
If you get the following error:
Indexing a copy of the Anaconda conda package channel is neither
necessary nor supported. If you which to add your own packages,
you can do so by adding them to a separate channel.
then you have to go to your "pkgs2" folder and delete the "_license" package.I don't know why it works but it does (saw the solution to the error from a google search).
You should see that 3 new files were added to the "pkgs2" folder after the indexing was completed. They are called ".index.json", "repodata.json" and "repodata.json.bz2".
You can now transfer the "pkgs2" folder and the "packagelist.txt" to another computer. You can also include a setup file for installing anaconda if the computer doesn't already have it installed. To install the new/updated packages offline, enter the following in a command prompt on the offline computer that has anaconda installed (once again, obliviously adjust the file names and locations accordingly) :
conda install --file C:\Users\myName\Desktop\OfflineInstall\packagelist.txt --channel file://C:\Users\myName\Desktop\OfflineInstall\pkgs2
If all went well, you should see something like this in your command prompt:
. And once the new packages are installed, you are done!
You might however, run into the following problem during Step 5 (this is just an example):
UnsatisfiableError: The following specifications were found to be in conflict:
- matplotlib 1.4.3 np19py27_1 -> pyparsing 2.0.3
- pyparsing 2.2.0 py27_0
Use "conda info <package>" to see the dependencies for each package.
Adjust your packages accordingly, either deleting conflicting ones you don't need, or updating the ones shown. Repeat from Step 4 and you should be ok at the end.
Hope this helps someone !

Will installing 2 conda on one computer mess up anything?

Currently I have installed a software called ArcGIS Pro 2.1 in C:\Program Files and it comes with a conda that is the only python I have on this computer. Now I need python for other projects and I don't want to go through IT department every time I need to install some python packages because I don't have the admin right.
Will installing another conda in a folder that I have right to edit mess up anything of the ArcGIS conda?
Or can I use the ArcGIS conda to create another python environment outside C:\Program Files in a folder I have right to edit?
Thanks.
You should be able to leverage the existing conda. You just need to make sure you always use the --prefix command to actually write the environment folder into a directory you can write to.
Check out the conda-create help docs. Look at the --prefix option
-- Edit --
There's actually an existing thread discussion about it. Check out this other SO link how to specify new environment location for conda create

How to backup Anaconda added packages?

I have Anaconda for Python 2, It came packed with a lot of useful packages. During my work, I have added several packages to it using conda install command. Now I have to format my system, and I want to backup/pack all the added libraries, either as full packages or even by knowing the installation command of each one.
I searched StackOverflow, I found one unanswered question with a similar problem, the question suggested conda list -e >file_list.txt to create a file contains all the installed packages, but this is not sufficient for me, I want Anaconda to determine which package is added by me, and by which command, or to pack the added packages in full.
Thanks for help.
I think you can find the solution you are looking for here.
Open the Anaconda prompt
Activate the environment you are interested in
Type conda env export > environment.yml
In the yml you will find all the dependencies and you can use it to create a new virtual environment as a copy of the current one.
For example, on the new/rebooted machine, you can do:
conda env create -f environment.yml

How can I install a conda environment when offline?

I would like to create a conda environment on a machine that has no network connection. What I've done so far is:
On a machine that is connected to the internet:
conda create -n python3 python=3.4 anaconda
Conda archived all of the relevant packages into \Anaconda\pkgs. I put these into a separate folder and moved it to the machine with no network connection. The folder has the path PATHTO\Anaconda_py3\win-64
I tried
conda create -n python=3.4 anaconda --offline --channel PATHTO\Anaconda_py3
This gives the error message
Fetching package metadata:
Error: No packages found in current win-64 channels matching: anaconda
You can search for this package on Binstar with
binstar search -t conda anaconda
What am I doing wrong? How do I tell conda to create an environment based on the packages in this directory?
You could try cloning root which is the base env.
conda create -n yourenvname --clone root
Short answer: copy the whole environment from another machine with the same OS.
Why
Dependency. A package depends on other packages. When you install a package online, the package manager conda analyzes the package dependencies and install all the required packages for you.
The dependency is especially heavy in anaconda. Cause anaconda is a meta package depends on another 160+ packages.
Meta packages,are packages do not contain actual softwares and simply depend on other packages to be installed.
It's totally absurd to download all these dependencies one by one and install them on the offline machine.
Detail Solution
Get conda installed on another machine with same OS. Install the packages you need in an isolated virtual environment.
# create a env named "myvenv", name it whatever you want
# and install the package into this env
conda create -n myvenv --copy anaconda
--copy is used to
Install all packages using copies instead of hard- or
soft-linking.
Find where the environments are stored with
conda info
The 1st value of key "envs directories" is the location. Go there and package the whole sub-folder named "myvenv" (the env name in previous step) into an archive.
Copy the archive to your offline machine. Check "envs directories" from conda info. And extract the environment from the archive into the env directory on the offline machine.
Done.
In addition to copying the pkgs folder, you need to index it, so that conda knows how to find the dependencies. See this ticket for more details and this script for an example of indexing the pkgs folder.
Using --unknown as #asmeurer suggests will only work if the package you're trying to install has no dependencies, otherwise you will get a "Could not find some dependencies" error.
Cloning is another option, but this will give you all root packages, which may not be what you want.
A lot of the answers here are not 100% related to the "when offline" part. They talk about the rest of OP's question, not reflected in question title.
If you came here because you need offline env creation on top of an existing Anaconda install you can try:
conda create --offline --name $NAME
You can find the --offline flag documented here
Have you tried without the --offline?
conda create -n anaconda python=3.4 --channel PATHTO\Anaconda_py3
This works for me if I am not connected to the Internet if I do have anaconda already on the machine but in another location. If you are connected to the Internet when you run this command you will probably get an error associated with not finding something on Binstar.
I'm not sure whether this contradicts the other answers or is the same but I followed the instructions in the conda documentation and set up a channel on the local file system.
Then it's a simple matter of moving new package files to the local directory, running conda index on the channel sub-folder (which should have a name like linux-64).
I also set the Anaconda config setting offline to True as described here but not sure if that was essential.
Hope that helps.
The pkgs directory is not a channel. The flag you are looking for is --unknown, which causes conda to include files in the pkgs directory even if they aren't found in one of the channels.
Here's what worked for me in Linux -
(a) Create a blank environment - Just create an empty directory under $CONDA_HOME/envs. Verify with - conda info --envs.
(b) Activate the new env - source activate
(c) Download the appropriate package (*.bz2) from https://anaconda.org/anaconda/repo on a machine with internet connection and move it to the isolated host.
(d) Install using local package - conda install . For example - conda install python-3.6.4-hc3d631a_1.tar.bz2, where python-3.6.4-hc3d631a_1.tar.bz2 exists in the current dir.
That's it. You can verify by the usual means (python -V, conda list -n ). All related packages can be installed in the same manner.
I found the simplest method to be as follows:
Run 'conda create --name name package' with no special switches
Copy the URL of the first package it tried (unsuccessfully) to download
Use the URL on a connected machine to fetch the tar.bz2
Copy the tar.bz2 to the offline machine's /home/user/anaconda3/pkgs
Deploy the tar.bz2 in place
Delete the now unneeded tar.bz2
Repeat until the 'conda create' command succeeds
Here's a solution that may help. It's not very pretty but it gets the job done. So i suppose you have a machine where you have a conda environment in which you've installed all the packages you need. I will refer to this as ENV1 You will have to go to this environment directory and locate it. It is usually found in \Anaconda3\envs. I suggest compressing the folder but you could just use it as is. Copy the desired environment folder into your offline machine's directory for anaconda environments. This first step should get your new environment to respond to commands like conda activate.
You will notice though that software like spyder and jupyter don't work anymore (probably because of path differences). My solution to this was to clone the base environment in the offline machine into a new environment that i will refer to as ENV2. What you need to do then is copy the contents of ENV2 into those of ENV1 and replace files.
This should overwrite the files related to spyder, jupyter.. and keep your imported packages intact.

How to get virtualenv to use dist-packages on Ubuntu?

I know that virtualenv, if not passed the --no-site-packages argument when creating a new virtual environment, will link the packages in /usr/local/lib/python2.7/site-packages (for Python 2.7) with a newly-created virtual environment. On Ubuntu 12.04 LTS, I have three locations where Python 2.7 packages can be installed (using the default, Ubuntu-supplied Python 2.7 installation):
/usr/lib/python2.7/dist-packages: this has my global installation of ipython, scipy, numpy, matplotlib – packages that I would find difficult and time-consuming to install individually (and all their dependences) if they were not available via the scipy stack.
/usr/local/lib/python2.7/site-packages: this is empty, and I think it will stay that way on Ubuntu unless I install a package from source.
/usr/local/lib/python2.7/dist-packages: this has very important local packages for astronomy, notably those related to PyRAF, STScI, etc., and they are extremely difficult and time-consuming to install individually.
Note that a global directory such as /usr/lib/python2.7/site-packages does not exist on my system. Note also that my global installation of ipython, scipy, etc. lets me use those packages on-the-fly without having to source/activate a virtual environment every time.
Naturally, I now want to use virtualenv to create one virtual environment in my user home directory which I will source/activate for my future projects. However, I would like this virtual environment, while being created, to link/copy all of my packages in locations (1) and (3) in the list above. The main reason for this is that I don't want to go through the pip install process (if it is even possible) to re-install ipython, scipy, the astro-packages, etc. for this (and maybe other) virtual environments.
Here are my questions:
Is there a way for me to specify to virtualenv that I would like it to link/copy packages in these two dist-packages directories for virtual environments that are created in the future?
When I eventually update my global installation of scipy, ipython, etc. in the two dist-packages directories, will this also update/change the packages that my virtual environment uses (and which it originally got during virtualenv creation)?
If I ever install a package from source on Ubuntu, will it go in /usr/local/lib/python2.7/dist-packages, or /usr/local/lib/python2.7/site-packages?
Thanks in advance for your help!
This might be a legitimate use of PYTHONPATH - an environmental variable that virtualenv doesn't touch, which uses the same syntax as the environmental variable PATH, in bash PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages in a .bashrc or similar. If you followed this path,
You don't have to tell your virtual environment about this at all, it won't try to change it.
No relinking will be required, and
That will still go wherever it would have gone (pip install always uses /usr/local/lib/python2.7/dist-packages/ for my Ubuntu) if you install them outside of your virtual environment. If you install them from within your virtual environment (while it's activated) then of course it'll be put in the virtualenvironment.
I'm just getting my head around virtualenv, but there seems to be an easier way than mentioned so far.
Since virtualenv 1.7 --no-site-packages has been the default behavior.
Therefore using the --system-site-packages flag to virtualenv is all that is needed to get dist-packages in your path - if you use the tweaked virtualenv shipped by Ubuntu. (This answer and this one give some useful history). I've tested this and it does work.
$ virtualenv --system-site-packages .
I agree with Thomas here - I can't see any action required in virtualenv to see the effect of updates in dist-packages.
Having tested that with python setup.py install, it does (again as Thomas said) still go to dist-packages. You could change that by building your own python, but that's a bit extreme.
PYTHONPATH works for me.
vim ~/.bashrc
add this line below:
export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages
source ~/.bashrc
In the directory site-packages, create a file dist.pth
In the file dist.path, put the following:
../dist-packages
Now deactivate and activate your virtualenv. You should be set.
What you want to achieve here is essentially add specific folder (dist-packages) to Python search path. You have a number of options for this:
Use path configuration (.pth) file, entries will be appended to the system path.
Modify PYTHONPATH (entries from it go to the beginning of system path).
Modify sys.path directly from your Python script, i.e. append required folders to it.
I think that for this particular case (enable global dist-packages folder) third option is better, because with first option you have to create .pth file for every virtualenv you'll be working in (with some external shell script?). It's easy to forget it when you distribute your package. Second option requires run-time setup (add a envvar), which is, again, easy to miss.
And only third option doesn't require any prerequisites at configure- or run-time and can be distributed without issues (on the same-type system, of course).
You can use function like this:
def enable_global_distpackages():
import sys
sys.path.append('/usr/lib/python2.7/dist-packages')
sys.path.append('/usr/local/lib/python2.7/dist-packages')
And then in __init__.py file of your package:
enable_global_distpackages()

Categories