スキップしてメイン コンテンツに移動

Create a virtual environment by reading the Conda environment configuration file in Anaconda Navigator

 Anaconda is a very useful environment for implementing Python for scientific calculations. Many people use Anaconda. However, if you keep installing and updating packages to the environment you have created, you may lose track of dependencies, and you may not be able to reproduce the environment on other computers.

To prevent this from happening, create and manage your preferences file well so that anyone can reproduce a virtual environment anywhere. In this article, we will show you how to create a virtual environment from an environment settings file using Anacnoda Navigator, a GUI rather than a CUI, so that even beginners can use it.

1. prepare environment setting file (yaml file)

The following is an example of an environment configuration file "env.yml". The file name can be arbitrary, but the extension should be "yml".

name: test-env
channels:
  - defaults
  - gurobi
dependencies:
  - python=3.8
  - gurobi
  - spyder
  - pandas
  - openpyxl

”name" is the name of the virtual environment. Give it a name that is easy to understand, such as the purpose of using this environment.

"channels" specifies the channels from which the package will be downloaded. Most packages are fine with "defaults", but certain packages require special channels to be specified. Make sure you know where the package you want to use can be downloaded from.

”dependencies" specifies the packages you wish to install in this environment. Basically, the latest version is installed, but if you want to specify a version, add "=" after the package name to specify the version.

Keep this preference file for each programming you create. One environment per programming. If you are using Git to manage your programming, you can naturally manage them together by adding the preferences file to your Git tracking.

2. import the preferences file

We will assume that you already have Anaconda installed on your PC.
Start Anaconda Navigator, which is installed with Anaconda. After that, follow the steps below. The numbers circled in red in the image below match the bulleted numbers below.

①. Select "Environments".
②. Select "Import".
③. Choose "Local drive" and set up the preferences file (edit "Name" if you want to change the environment name).
④.  Select "Import".


This will start the creation of the new virtual environment (installation of packages, etc.). It will take some time, after which the new environment will be added to the virtual environment list.

3. if you want to install a new package in your environment

It is installed using Conda, a package that is included by default in the Anaconda virtual environment.

conda install [Package-Name]

After this, do not forget to add the installed packages to the "dependencies" section of the configuration file. If you forget to do so, you will not be able to reproduce the environment on other computers.

コメント

このブログの人気の投稿

Sourcetreeを使って、特定のブランチやタグをローカルに再現する方法 - フェッチ(fetch)を使おう -

今や、プログラミング開発に欠かせない存在となったGit。上司や同僚から「修正版をブランチ"devlop"にプッシュしたから、それを反映させてね」「安定版はタグ”v1.2”だから、よろしくね」と言われることもあるでしょう。その時に「どうやってブランチやタグって変えたらいいかわからない」「そもそも言われたブランチやタグが見当たらないんだけど…」となったら、とても困ってしまいます。そんなときにどうしたらいいか、本記事では Sourcetree を使用している環境を想定して、おすすめの手順を紹介します。環境によって具体的な操作は異なるものの、手順自体は一緒です。基本的には、 フェッチ(fetch)をしてリモートリポジトリをちゃんと確認 すればよいのです。 1. コミットしていないファイルをなくそう まず、Sourcetreeのコミット一覧に「コミットされていない変更があります」とあるかどうか確認します(下記図赤下線参照)。「コミット」の箇所に青マークでコミットされてないファイル数も表示されます(下記図赤枠参照)。その変更をコミットするか、破棄するかを行い、コミットされていない変更がない、きれいな状態にしましょう。そうしないと、後の作業中にエラー等が発生して進まなくなってしまいます。 2. フェッチしよう フェッチとは、 リモートリポジトリの最新の履歴の取得だけを行う 操作です。これによって、自分から見えていなかったブランチ・タグが見えるようになります。ブランチへのマージが伴うプッシュやプルと異なり、単に履歴を取得するだけなので、意図しないコミットが発生しません。安心して実施しましょう。Sourcetreeでは下記図赤枠部分をクリックすれば、フェッチを行うことができます。 その際出てくるポップアップで「すべてのリモートからフェッチ」「全てのタグを取得」をチェックしてください(下図赤下線参照)。 3. 目的のブランチ・タグを見つけよう 目的のブランチ・タグを見つけるのに便利なのが、Sourcetreeの左側領域にある「ブランチ」「タグ」「リモート」「スタッシュ」です。目的のブランチを探したい場合は「リモート」をクリックして現れる「origin」をさらにクリックしましょう。これでリモート上にあるブランチの一覧を見ることができます。タグの場合は「タグ」をクリック

GitHubのプライベートリポジトリからクローンできてもプッシュできないときの対処法 - Write権限の有無を確認しよう -

 以前、 SourceTreeを使って、GitHubのプライベートリポジトリにアクセスする方法 を記事にしました。その際、プライベートリポジトリからクローンできても、プッシュできない!という現象に遭遇するときがあります。下記のように、”そんなリポジトリは存在しないよ”というエラーが表示されます。 remote: Repository not found. もし、そのリポジトリがOrganaizationに属するものならば、 あなたのアカウントの権限が”Read”のみで、”Write”になっていない 可能性があります。権限に関してOrganaizationの管理者に確認してもらいましょう。 管理者がどういうルールで権限を決定しているのかによって、対応が変わるかと思います。一番手っ取り早い対応は、全てのメンバーのデフォルト権限を”Write”に変えてしまえばよいでしょう。Organaizationのページの右上にある「Settings」をクリックし、左に表示されるメニューから「Member privileges」を選択します。すると、左下のような画面が表示されますので、赤枠で囲っている「Base permissions」のプルダウン部分を"Write"に変更してください。 その他に、リポジトリ毎にアクセスできるメンバーを管理したい場合もあるかと思います。その際にはリポジトリの「Settings」→「Manage access」を選択し、設定等を変更してください。

SourcetreeでGitHubのプライベートリポジトリをクローンする

Sourcetree は、初心者でも操作しやすいgitのGUIツールです。基本無料のソフトなので、あらゆる人が導入・利用しやすいという利点を持ちます。ですが、GitHubで非公開、つまりプライベートのリポジトリを取り扱う場合には、初期設定に一工夫が必要です。 本記事で紹介する設定の流れは以下の通りです。前提として、1)GitHubのアカウントとプライベートリポジトリを既に所有しているが、2)Sourcetreeを自身のPCにまだインストールしていない状態を想定しています。 GitHubのPersonal access tokenを作成する Sourcetreeを自身のPCにインストールする SourcetreeにGitHubのアカウントを登録する プライベートリポジトリをクローンする 今回はPersonal access tokenという手法を用います。秘密鍵や公開鍵が必要なSSH接続とは異なり、アカウント名とパスワードのみで認証できます。そのため、アカウント管理がより重要となりますので、 2段階認証 でより強力なアカウント保護をおこなうことをお勧めします。また、本記事で紹介するのはWindowsでの利用です。Macでもほぼ同じ流れになるかと思いますが、試してはいませんのでご了承ください。 1. GitHubのPersonal access tokenを作成する GitHubのPersonal access tokenとは、GitHub API またはコマンドラインを使用するときに GitHub への認証でパスワードの代わりに使用できる機能です。このトークンを自身のGitHubアカウントで作成することで、Sourcetreeからプライベートリポジトリにアクセスすることができるようになります。具体的な作り方は下記のGitHub公式ページををご参考ください。 Personal access tokenの作成方法: https://docs.github.com/ja/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token ポイントは手順8に書かれているS