Anacondaで仮想環境ごとにライブラリを管理する
仮想環境のおさらい
詳しくはAnacondaで仮想環境を構築するを参照してください。
仮想環境を作り、環境を切り替えます。
conda create -n testenv python=3.5 conda acivate
環境を切り替えた状態でパッケージをインストールすれば、切替先の環境にのみパッケージがインストールされます。
パッケージ管理
パッケージのインストール
conda install numpy
インストールしたパッケージの確認
conda list
パッケージの更新
conda update numpy
パッケージの削除
conda uninstal numpy
Anacondaで仮想環境を構築する
私的まとめ
なぜ仮想環境?
用途に応じて専用の実行環境を作成し、切り替えるのが一般的です。
この作成した実行環境のことを仮想環境と言いいます。
仮想環境を使うことで以下のメリットがあります。
- グローバルのPython環境を汚さない
- Python2.x, 3.xを使い分ける
- 環境ごとに異なるバージョンのモジュールを入れる
Anacondaで仮想環境
Windows7かつAnacondaが入っている前提です。
詳しくはAnacondaとVSCodeでPython環境構築@Windows7を参照してください。
仮想環境を作る
Pythonのバージョンが3.5で「testenv」という名前の仮想環境を構築する場合は下記コマンドを実行します。
conda create -n testenv python=3.5
追加でパッケージを入れる場合はパッケージ名を追記します。
conda create -n testenv python=3.5 numpy scipy
フルパッケージの環境を作る場合は最後をanacondaにします。
conda create -n pu35 python=3.5 anaconda
仮想環境を消す
conda remove --name testenv --all
仮想環境の確認
conda info --envs # conda environments: # base * C:\Users\Someone\AppData\Local\Continuum\anaconda3 testenv C:\Users\Someone\AppData\Local\Continuum\anaconda3\envs\testenv
現在の環境に*が付与されます。
環境を切り替える
testenvに環境を切り替える場合は下記コマンドを実行します。
activate testenv
環境から抜ける
下記コマンドを実行すると切り替えた環境から抜けることが出来ます。
deactivate
AjaxのCSRF対策
こちらに移行しました。
検証属性のエラーメッセージを一元管理する
こちらに移行しました。
検証属性を自作する
こちらに移行しました。
TypeScript環境構築
今回はVisual Studioを使わない場合のTypeScript開発環境を構築してみます。
TypeScriptのインストール
npmでTypeScriptをインストールします。
npm install typescript -global
これでTypeScriptコンパイラを利用できます。
初期化
プロジェクトルートでまず下記コマンドを実行します。
tsc --init
これでtsconfig.jsonが作られます。
tsconfig.json
{ "compilerOptions": { /* Basic Options */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation: */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "declaration": true, /* Generates corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ // "outDir": "./", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */ // "importHelpers": true, /* Import emit helpers from 'tslib'. */ // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ "strict": true /* Enable all strict type-checking options. */ // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ /* Additional Checks */ // "noUnusedLocals": true, /* Report errors on unused locals. */ // "noUnusedParameters": true, /* Report errors on unused parameters. */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ /* Source Map Options */ // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ /* Experimental Options */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ } }
tsconfig.jsonでコンパイルの詳細な設定を行うことが出来ます。
tsファイルのトランスパイル
例えばapp.tsをコンパイルする場合は下記コマンドを実行します。
tsc app.ts
app.tsからapp.jsが生成されます。