Create Virtual Environment, Install & Load Packages, Code within Jupyter Notebook or Spyder

To install Anaconda packages you must create a new virtual environment pointing to your U: drive. Information about the U: drive for your CCSS Research Support computing account can be found inside the link below.
CCSS-RS Workspace

Confirmed process working on RSCH105.ciserrsch.cornell.edu

Creating Virtual Environment

GREEN TEXT INDICATES A COMMENT. DO NOT ENTER GREEN TEXT INTO YOUR ANACONDA PROMPT. Regular colored text use as reference to enter into anaconda prompt
CONNECT TO CCSS-RS SERVERS. OPEN “ANACONDA PROMPT” FROM START MENU
Do work within U: drive
U:
Set directory using “cd” command. Directory is folder on U: drive where my work is. In example below
cd Documents
Create new folder inside Documents.
mkdir "conda_dir"
cd "conda_dir"

Create virtual environment. Will be created where directory was set to above. A folder “myenv” from code below will be created within “U:\Documents\conda_dir”
conda create --prefix ./myenv python=3.8
y

Activate newly created virtual environment below
activate "U:\Documents\conda_dir\myenv"
y

Install packages ‘gensim’ and ‘tensorflow’ as example. Use pip command to install
pip install gensim
pip install tensorflow

Incorporating Jupyter Notebook

For this demo I will initiate jupyter notebook and use a package with sample code.
Begin by following instructions above to create virtual environment. Install your desired package prior to starting jupyter notebook.
Use pip command to install libraries to your virtual environment
pip install keras
pip install tensorflow

Must install ipykernel to initiate from jupyter notebook from Anaconda command prompt
conda install ipykernel
Must install “jupyter” library to initiate from Anaconda command prompt
pip install jupyter
If pip install jupyter gives kernel dead. use pip uninstall jupyter, then use conda
conda install jupyter
Will use “random2” package for my sample code
pip install random2
Initiate jupyter notebook with command below
jupyter notebook
You may have .ipynb files scattered all over your filing system. You can initiate your environment from any folder so long as you specify the location
jupyter notebook --notebook-dir U:/Documents
Command above opens Jupyter with Documents as home directory. Now I can access any .ipynb files inside all folders within Documents. This can be done with Desktop, Downloads, ect.
Some information gotten from following link.
pythonforundergradengineers.com

Incorporating Jupyter Lab

Begin by following instructions above to create virtual environment. Use pip command to install libraries to your virtual environment
pip install jupyterlab
type ‘jupyter lab’ to initiate. Make sure to open using Firefox or Microsoft Edge browsers, Internet Explorer does not support jupyter lab
jupyter lab
Take localhost link in screenshot below and paste into acceptable browser, Firefox or Microsoft Edge. Below is example of localhost URL from screenshot below:
http://localhost:8888/?token=6f0251d09cbd560c97b38faacfd4ddb040dec8d167c770c0

  • Jupyter lab opened in acceptable browser

Sample Program

Once jupyter notebook has opened within a web browser use the program below to test. This program is dependent on the library “random2”, install in commands above.


#!/usr/bin/env python
import random # Get a random number generator.
NTRIALS = 10000 # Enough trials to get an reasonably accurate answer.
NPEOPLE = 30 # How many people in the group?
matches = 0 # Keep track of how many trials have matching birthdays.
for trial in range(NTRIALS): 12# Do a bunch of trials...
taken = {} # A place to keep track of which birthdays
# are already taken on this trial.
for person in range(NPEOPLE): 60# Put the people’s birthdays down, one at a time...
day = random.randint(0, 365) # On a randomly chosen day.
if day in taken:4
matches += 1 # A match! # No need to look for more than one.
taken[day] = 1 # Mark the day as taken.
print("The fraction of trials that have matching birthdays is", float(matches)/NTRIALS)

Using Spyder

Open Anaconda prompt.
Activate directory using steps at beginning of article.

Must install ipykernel to initiate from jupyter notebook from Anaconda command prompt
conda install ipykernel

pip install spyder
pip install jedi
conda install ipykernel
Use pip command to install other libraries to your virtual environment
pip install random2
Initiate Spyder application with command below
spyder

Run test program dependent on random2 library.

#!/usr/bin/env python
import random # Get a random number generator.
NTRIALS = 10000 # Enough trials to get an reasonably accurate answer.
NPEOPLE = 30 # How many people in the group?
matches = 0 # Keep track of how many trials have matching birthdays.
for trial in range(NTRIALS): 12# Do a bunch of trials...
taken = {} # A place to keep track of which birthdays
# are already taken on this trial.
for person in range(NPEOPLE): 60# Put the people’s birthdays down, one at a time...
day = random.randint(0, 365) # On a randomly chosen day.
if day in taken:4
matches += 1 # A match! # No need to look for more than one.
taken[day] = 1 # Mark the day as taken.
print("The fraction of trials that have matching birthdays is", float(matches)/NTRIALS)