Setting up TensorFlow Environment and Using Jupyter in Python
Recommended article: 【Python】 Python Table of Contents
1. Overview
3. Anaconda Prompt (anaconda3)
5. Sharing Anaconda Environment
1. Overview
⑴ TensorFlow, Keras, and others are created for implementing artificial neural networks in Python.
⑵ A version compatible with R has been available since 2017.
⑶ MacBooks come with Terminal, so these are naturally installed.
⑷ Anaconda has been offering its services for a fee to users in businesses or government organizations with more than 200 employees since September 2020.
2. Installing Anaconda
⑴ Windows
① Step 1: Installation website: www.anaconda.com/products/individual
② Step 2: Download and installation guide: www.guru99.com/download-install-r-rstudio.html
③ Step 3: Finally, the following is installed:
○ Jupyter Notebook (anaconda3)
○ Anaconda Navigator (anaconda3)
○ Spyder (anaconda3)
○ Anaconda PowerShell Prompt (anaconda3)
○ Anaconda Prompt (anaconda3)
⑵ For Linux
⑵ Linux
① Step 1. Miniconda : Install Miniconda3 Linux 64-bit from here
② Step 1 - Method 1
○ Step 1. Download the file from the link
○ Step 2. Move the
.sh
file to the desired directory in Linux: You can use commands likecp
,rsync
,scp
○
cp [OPTION] [SOURCE] [DESTINATION]
: Copy and paste the file
○
-r
: Copy entire subdirectories and files
○
-p
: Copy preserving owner, group, permissions, and timestamp information
○
scp
: Low chance of file corruption
○
scp -P **${port}** **${srcFile}** **${id}** @ **${destIP}** : **${destPath}**
○
**${port}**
: Port number of the destination
○
**${srcFile}**
: Path of the file to be moved from source. Example: “~/Downloads/file.txt”
○
**${id}**
: ID of a user at the destination
○
**${destIP}**
: IP address of the destination. Example: #.#.#.# (IPv4)
○
**${destPath}**
: Directory address to store the file at the destination. Example: ~/DATA1/1.txt
○
rsync
(remote sync): Faster speed
○
rsync --rsh=’ssh -p **${port}** ’ -avzhP **${srcFile}** **${id}**@**${destIP}** : **${destPath}**
○
**${port}**
: Port number of the destination
○
**${srcFile}**
: Path of the file to be moved from source. Example: “~/Downloads/file.txt”
○
**${id}*
*: ID of a user at the destination
○
**${destIP}**
: IP address of the destination. Example: #.#.#.# (IPv4)
○
**${destPath}**
: Directory address to store the file at the destination. Example: ~/DATA1/1.txt
③ Step 1 - Method 2
○ Step 1. Copy the download link for Miniconda3 Linux 64-bit from the link above
○ Step 2. Use the
curl -o
command
curl -o my.sh "https://repo.anaconda.com/miniconda/Miniconda3-py38_23.3.1-0-Linux-x86_64.sh"
④ Step 2. Execute
.sh
file, Processing.sh
file
○ File execution: To execute a
.sh
file, you can do./[My_File].sh
,sh [My_File].sh
, orbash [My_File].sh
.
○ File modification: There are two kinds of shells,
zsh
andbash
.
○
$ vi script.sh
○
$ nano backup
○
$ sudo apt-get install zsh
○ Granting permissions
○ Example: $
chmod +x run-md5sum.sh
⑤ Step 3. Run the Miniconda .sh file and read the terms and conditions to proceed with the installation. Press Enter to continue.
⑥ Step 4. Respond to the license agreement:
○ The response must be “yes” at the first prompt.
⑦ Step 5. Respond to the second “yes”.
○ Answer whether to perform the install init.
○ The default is “no,” but choosing “no” will prevent
/bin/bash
from functioning properly.
○ It is recommended to explicitly enter “yes.”
⑧ Step 6. Reboot your Linux system or run
/bin/bash
.
○ If
/bin/bash
is not working, executesource ~/.bashrc
.
3. Anaconda Prompt (anaconda3)
⑴ For Windows
① Run anaconda3: It is recommended to run it with administrator privileges.
② Install packages and set up virtual environments: Virtual environments are bundles of packages for managing package versions.
Here's the translation of the provided text from Korean to English:
# 0. Move to the directory to install
cd C:/Users/sun/
# 1. Examples of package installation
conda install r-essentials
conda uninstall r-essentials
conda install -c conda-forge r-essentials=4.0
conda install tensorflow
conda install -c conda-forge keras
python -m pip install --upgrade pip
# 2. Update base
conda update -n base conda
conda update --all
# 3. Create virtual environments tensorflow, tensorflow2
conda create --name tensorflow python=3.7
conda create --name tensorflow2 python=3.7
# 4. Check the list of virtual environments (base is default)
conda info --envs
# 5. Check Python version
python --version
# 6. Update virtual environment tensorflow
pip install --ignore-installed --upgrade tensorflow
# 7. Delete virtual environment tensorflow2
conda remove --name tensorflow2 --all
# 8. Activate virtual environment tensorflow
activate tensorflow
# 9. Clean up unused packages
conda clean --all
⑵ For Linux
① In Linux, once conda is installed, you can easily see if it’s installed with (base) in front of the name.
② Base is the name of the default environment, and it changes to a different environment name (see below) when a new environment is installed.
③ Note that environments are stored in ./miniconda3/envs/.
⑶ Troubleshooting
① Avoid using Korean characters in usernames or file paths.
② (Possible) Method 1: Create a new user account with an English username and file path: Settings → Family & other users → Add someone else to this PC.
③ (Not recommended) Method 2: Changing the username: www.tabmode.com/windows10/win10-user-name-change.html#gsc.tab=0
○ Using this method may lead to the “Can’t sign in to your Windows account” issue.
④ (Possible) Method 3: AccountProfilerFixer.exe: m.blog.naver.com/dupen/222081638329
4. Installing Jupyter
⑴ Step 1. Run anaconda3 and enter the following code (reference link: wikidocs.net/25280)
activate tensorflow
pip install jupyter
jupyter notebook
⑵ Step 2. jupyter lab (optional)
pip install jupyterlab
jupyter lab
① jupyter lab is convenient for directory management.
② jupyter lab makes it easy to upload, delete, and rename files.
⑶ Step 3. Installing packages (e.g., tensorflow)
# install tensorflow package
### tensorflow package version should be matched to python version
pip install tensorflow
# You can specify the exact version of the package for the environment
pip install keras==2.2.4
# You can even remove previous packages
pip uninstall keras
pip install keras
# execute jupyter
jupyter notebook
① The author prefers installing as mentioned above.
② However, installing tensorflow.keras instead of keras may be beneficial for version management.
⑷ Step 4. Installing jupyter ipykernel
pip install ipykernel
python -m ipykernel install --user --name JEONGBIN_ENV --display-name JEONGBIN_ENV
① Replace JEONGBIN_ENV with your environment name.
② You can easily change the environment of a launched Jupyter notebook through this.
⑸ Step 5. Running Jupyter Notebook
① Step 5-1. Click New in the upper right corner of the browser → Python 3.
○ If R is installed in Jupyter, R will also be displayed under Python 3 in Step 1.
② Step 5-2. Enter code → Run.
③ Jupyter Notebook Key Shortcuts
○ a: Add a new cell above the current cell
○ b: Add a new cell below the current cell
○ z: Undo the action
○ Shift + Up/Down Arrow: Select multiple consecutive cells
○ Command (⌘) + Enter: Execute the current cell
○ Command (⌘) + f: Find a string
○ Command (⌘) + s: Save
5. Sharing Anaconda Environment
⑴ Environment File (.yml
): The simplest method
① step 1. Extracting the environment file: Extract a
.yml
file from the environment named JEONGBIN_ENV.
conda activate JEONGBIN_ENV
conda env export > environment.yml
# Now you can see environment.yml at the mother directory.
② step 2. Creating an environment using the environment file
conda env create -f environment.yml
③ Troubleshooting 1. Solving environment: failed. ResolvedPackageNotFound: (ref)
○ It seems that using the above conda command in Windows essentially leads to an error.
○ Method 1. Execute after removing the build info.
○ Method 2. Execute after removing the platform-specific version tag.
⑵ Docker: The most recommended method
⑶ Binder
⑷ requirements.txt
: You can list packages using commands like pip list
and pip freeze
. These packages can be saved to a requirements.txt
file, which allows for the bulk downloading of packages later using the following command.
pip install -r requirements.txt
⑸ Sharing an environment through a GitHub repository.
# Example
conda create -n CellDART python=3.8
conda activate CellDART
pip install git+https://github.com/mexchy1000/CellDART.git
python -m ipykernel install --user --name CellDART --display-name CellDART
⑹ README.md
: Simply indicate specifications as plain text.
⑺ To run a .sh
file, you can do so by using ./[My_File].sh
, sh [My_File].sh
, or bash [My_File].sh
.
bash ./MyEnvironment.sh
⑻ renv
: Dependency management system for R
⑼ Mamba
: Manager for Anaconda. Can use bioconda channel. Available for Python, R.
⑽ SnakeMake
: Python-based pipeline development, and workflow management system.
⑾ Singularity
: A container like Docker. .sif
file indicates Singularity container image file.
① Command:
singularity exec myFile.sif original_command [argument1] [argument2] ···
⑿ Podman
: A container like Docker
Input: 2021.02.28 17:36
Modified: 2023.05.22 15:04