How to Run DeepSeek R1 Locally in Python? Learn to Install required Python libraries to run DeepSeek R1 in Python on your Desktop
It is very easy for the developers to run the DeepSeek R1 model from the Python code and get output from the model. In this tutorial I am going to show you all these step-by-step. I will also share with you the Python source code to run the DeepSeek R1 model locally from the Python program. As a Python programmer you will be able to use the DeepSeek R1 model from your Python program.
Here is the detailed video instruction of running DeepSeek R1 from Python code using the Python ollama library. In the video you will find all the steps of chatting with the DeepSeek R1 model using the Python code. Here is the video:
Step 1: Install Ollama
First of all you have to install Ollama on your system. If you are using Windows then check this tutorial for installing Ollama on Windows computer: Install and use DeepSeek-R1 Locally..
Step 2: Install Python Latest Version
The next step is to install the latest version of Python on your operating system. You can follow these tutorials:
Step 3: Download DeepSeek R1 Model
Next you have to download the DeepSeek R1 Model on your computer. Run the following command in the terminal to download DeepSeek R1 model:
ollama run deepseek-r1:7b
Above command will download 7B model on your computer.
Step 4: Run DeepSeek R1 Locally in Python
First of all create a Python Virtual environment with following command:
python3 -m venv env1
Then activate the environment with following command:
env1\Scripts\activate
Next install ollama Python library using following command:
pip install ollama
Find out the available models in your system by running following command:
ollam list
Note down the name of the model and add in the python file which we will create to test the DeepSeek R1 model.
Now the final step is to create a Python file say test.py in the diretoy where you created your Python virtual environment. Here is the complete code of the program:
import ollama
desiredModel='deepseek-r1:7b'
questionToAsk='How to make good tea?'
response = ollama.chat(model=desiredModel, messages=[
{
'role': 'user',
'content': questionToAsk,
},
])
OllamaResponse=response['message']['content']
print(OllamaResponse)
Run the above python file and you will get following output:
(env1) (base) C:\tutorials\DeepkSeekR1\Proj2>python test.py
<think>
<think>
To make good tea, follow these simple steps:
1. **Choose the Right Type of Tea**: Depending on your preference, select black, green, oolong, or matcha tea.
2. **Infuse for the Right Amount of Time**:
- Oolong and matcha: 1-2 minutes.
- Black tea: 3-4 minutes.
- Green tea: 2-3 minutes.
3. **Use the Correct Size teapot**: A larger pot allows for longer infusing times, while a smaller pot is better for shorter, stronger brews.
4. **Use High-Quality Tea Leaves**: The quality of your tea will affect the flavor and aroma.
5. **Boil Water**: Fill your teapot with boiling water (180-200°F).
6. **Insert the Teaspoon**: Drop the tea leaves into the pot, making sure they are fully submerged.
7. **Let It Steep**: Let the tea steep for the recommended amount of time.
8. **Check the Color and Flavor**: If your tea is too pale or bitter, add a splash of water to infuse more color.
9. **Serve**: Garnish with honey, sugar, or lemon if desired, and enjoy!
For black tea:
- Use fewer leaves (about 1 ounce per cup).
- Boil water gently for 3 minutes.
- Let it steep for 4-5 minutes.
- Remove the tea bag before serving.
For green tea:
- Use about 2 grams per cup (or a teaspoon of powdered tea, if using).
- Boil water gently for 3 minutes.
- Let it steep for 2-3 minutes.
- Check for excess bitterness and add water if needed.
For oolong or matcha:
- Steep for shorter times (1-2 minutes) to maintain the grassy flavor.
- Add a splash of cold water if your tea is too pale.
experiment with different types, sizes, and amounts of tea to find your favorite!
(env1) (base) C:\tutorials\DeepkSeekR1\Proj2>
Here is the screen shot of the program execution:

In this tutorial I have showed you how to run DeepSeek R1 model from your Python program.
Related Tutorials