In summary, here is the proposed order to properly set up your work in Python (one-time job).
- Install Python (e.g. 3.7)
- Create a virtual environment to house all the packages needed in your project
- Install desired packages in your virtual environment
- Jupyter (for the notebook experience)
- Install all desired packages
- Verify setup, and you're done!
No Anaconda needed whatsoever!
To be safe, contact your IT or seek one-on-one help from a colleague to ensure you install Python right.
Here are some things to check off at the end of the installation, to verify.
- Make sure python can be accessed in your terminal (command prompt/bash/etc.) using the command
python
- If no luck, then try
python3
instead. This would be an issue if you had multiple versions of Python. - If still no luck, then perhaps Python was not added to your system's "PATH".
- If no luck, then try
- For our purposes, we are interested in more recent versions of Python. For example, 3.6 or later might be okay, but I'm not sure. I use 3.7 and it works well.
It is recommended that for each project you work on, you create a separate virtual env. to better manage your Python packages.
For detailed info on creating and using virtual envs, Corey Schafer's videos are an excellent resource:
Here's a summary for Windows users:
Say we want to create a virtual environment called ml_demos
. This will be created as a folder on your machine.
-
Open Command Prompt and change to the desired directory
- Ideally, this is a directory that's easy to get to (e.g.
...\YOUR_PROJECT\
) - To change directories, simply enter
cd
followed by the full path (e.g.cd C:\...\YOUR_PROJECT\
) - Ensure that the path has been updated.
- Ideally, this is a directory that's easy to get to (e.g.
-
Enter
python -m venv ml_demos
- Check that a folder called
ml_demos
has been created in your directory. If so, continue.
- Check that a folder called
-
Activate your virtual environment (i.e. enter into it):
ml_demos\Scripts\activate.bat
- You should now see
(ml_demos)
appear on the left
-
Once
ml_demos
is activated (above), enterpip list
. These two packages should appear:
pip
setuptools
If a message appears below them indicating that you are using an old version, not to worry. Follow the next step to resolve.
-
Upgrade
pip
andsetuptools
to ensure smooth package installation later:pip install --upgrade pip
... (refer to Appendix below in case an error appears)pip install --upgrade setuptools
-
Deactivate (i.e. leave) this environment:
deactivate
Moving forward, every time you want to activate/deactivate your virtual environment:
(Again assuming Windows) Open Command Prompt and change to your project directory (like above)
- Activate:
ml_demos\Scripts\activate.bat
- Deactivate:
deactivate
-
Activate
ml_demos
as described previously. -
Install Jupyter:
pip install jupyter
pip install jupyterlab
-
Install PyTorch (if you need it):
-
Scroll to the "Start Locally" section on PyTorch's website.
- Select your OS (Linux, Mac, or Windows)
- For package, select Pip
- For CUDA, this is where it gets tricky. Please refer to Appendix below.
-
Once you selected all of the above, a command should appear in the box below
next to "Run this Command". Copy all of this.
-
Return to your command prompt (or bash, etc.) and paste and run command there.
- This will take some time (mainly to download ~500 MB)
-
... and you're done installing PyTorch!
-
-
Install all remaining packages all at once. In our case, those are all stored in a file called
requirements.txt
.-
Make sure
requirements.txt
is in the current directory -
Run this command:
pip install -r requirements.txt
-
Hopefully, you have been able to install everything as described above. Here's one way to test things:
- Testing packages in command-line Python:
- Again, make sure
ml_demos
(virtual environment) is activated - Enter Python by simply entering
python
- Try running this command:
import torch, numpy, matplotlib, pandas, sklearn, statsmodels, control, tqdm, gym, Box2D, stable_baselines3
. - Exit python
- Again, make sure
- Try using JupyterLab:
- Again, make sure
ml_demos
(virtual environment) is activated - Run this command:
jupyter lab
. This should open a new browser window/tab after a few seconds. - Have fun with JupyterLab ... (e.g. open notebook, type some Python code, run cells)
- For more info, YouTube and Google are your best friend!
- Once you're done using JupyterLab, it is good practice to end your session as follows:
- Shut down any active kernels
- Click on the stop icon on the left then select which notebooks/kernels to shut down (all)
- THEN, Shut down entire session: File menu > Shut down
- You'll now notice, when you go back to your command prompt/bash (which you used to open JupyterLab), that you can type commands again. Before shutting down your session, you are not able to do that.
- Shut down any active kernels
- Again, make sure
-
In step 2, if you encountered an error during
pip install --upgrade pip
and it looked like:"ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied:"
then you can check if this is actually a problem or not by entering
pip list
. Ifpip
appears with no warning message, then you're good to go and you can disregard that error message. -
In step 2, to decide which CUDA version (for installing PyTorch), I recommend you contact your IT.
-
If you're still not sure what option to select, None is a good option (you simply won't be using any GPU).
-
If you are confident you can do it by yourself, here's how to approach it on Windows:
-
If you don't have GPU on your machine, don't proceed.
-
If you're using Nvidia, go to Nvidia Control Panel > Help menu > System Information > Components Tab.
In my case, it showed "NVIDIA CUDA 11.1.70 driver" next to "NVCUDA64.DLL". This indicates that I should select 11.x
on PyTorch's website.
-
-