الدرس 1 · الوحدة 1: Python for AI
Setting up your AI workstation (conda, VS Code, Jupyter)
Learn it with your tutor on the live board
أهم نقاط الفيديو
It works on my machine
- A teammate shares a notebook that analyzes last month's sales
- On your laptop: import errors, version clashes
- An afternoon lost before you see one number
- The fix: a repeatable workstation setup
What you will learn
- Create isolated Python environments with conda
- Connect VS Code and Jupyter to the right environment
- Capture your setup in one file anyone can rebuild
One project, one environment
- An environment = its own Python + its own packages
- Projects can't break each other
- Broken? Delete it and rebuild
- Keep "base" almost empty
Install Miniforge
- Small conda installer, free for any use
- Uses the community "conda-forge" package channel
- Check it:
conda --version - Then:
conda info --envs
Create the course environment
conda create -n aicourse python=3.11
conda activate aicourse
conda install pandas numpy scikit-learn \
matplotlib plotly jupyterlab ipykernel Worked example: a quick smoke test
import sys
import pandas as pd
import sklearn
print(sys.executable)
print(pd.__version__, sklearn.__version__)
df = pd.DataFrame({
"order": [1, 2, 3],
"total": [20.5, 13.0, 42.0],
})
print(df["total"].sum()) Point VS Code at the environment
- Install the Python and Jupyter extensions
- Command palette: "Python: Select Interpreter"
- Choose the aicourse environment
- Check the status bar before you run code
Jupyter kernels
- A kernel is the Python process behind a notebook
- Register the environment once (command below)
- Pick "aicourse" in the kernel picker
- Installed a package? Restart the kernel
python -m ipykernel install --user --name aicourse
Capture the setup in a file
conda env create -f environment.yml
name: aicourse
channels:
- conda-forge
dependencies:
- python=3.11
- pandas
- scikit-learn
- jupyterlab
- ipykernel Common mistake: mixing pip and conda
- Running
pip installwith no environment active - pip overwriting packages conda installed
- Rule: conda first, pip only when needed
- When in doubt:
where python/which python
Recap
- One project, one conda environment
- VS Code and Jupyter must point at it
- environment.yml makes it rebuildable anywhere
Your exercise
- Install Miniforge and create the aicourse environment
- Run check_env.py from VS Code and from a notebook
- Export your environment.yml
- Delete the environment, then rebuild it from the file
النص المكتوب
It works on my machine. Picture this. A teammate sends you a notebook that analyzes last month's online sales. It runs perfectly on their laptop. On yours, the very first cell fails with an import error, and once you fix that, another library complains about a version mismatch. An afternoon disappears before you've looked at a single order. If you've ever deployed software, you know this story as "it works on my machine". It happens constantly in data and AI work, because these libraries depend on each other in very specific ways. The fix isn't heroic debugging. It's a workstation that's set up the same way every time, with isolated environments you can rebuild in minutes. That's what we'll build in this lesson, and it's the foundation for everything else in the course.
What you will learn. By the end of this lesson you'll be able to do three things. First, create isolated Python environments with a tool called conda, and install packages into them safely. Second, connect VS Code and Jupyter notebooks to the correct environment, so the code you run actually uses the packages you think it uses. Third, capture your whole setup in a single small file that a colleague, a server, or future you can rebuild exactly. None of this is glamorous, but it removes a whole category of frustrating problems from the rest of the course, so you can spend your time on the interesting parts.
One project, one environment. The core idea is isolation. Instead of installing everything into one shared Python on your computer, each project gets its own environment, with its own Python version and its own set of packages. Think of it like separate virtual machines or containers, just much lighter. Your old reporting project can stay on an older version of pandas while your new AI project uses the latest release, and neither one can break the other. If an environment ever gets into a bad state, you don't repair it, you delete it and rebuild it. A good habit is to leave the default environment, called base, almost empty. Think of base as the toolbox, and each project environment as a separate workbench.
Install Miniforge. To get conda, I recommend Miniforge. It's a small, free installer that uses a package source called conda-forge by default, where the community maintains well-tested builds of thousands of data science libraries. Many of these libraries include compiled code under the hood, and getting matching versions of all that compiled code is exactly what conda is good at. Download the installer for your operating system and run it with the default options. Then open a new terminal and run conda dash dash version, followed by conda info dash dash envs. You should see a version number and a single environment called base. If the command isn't found, the installer didn't hook conda into your shell, and rerunning its init step usually fixes that.
Create the course environment. Now let's create the environment we'll use throughout this course. The first command creates an environment named aicourse with Python three point eleven. The second activates it, and you'll see its name appear at the start of your prompt, which is your reminder of where you're working. The third command installs our core toolkit in one go: pandas and NumPy for data, scikit-learn for machine learning, two plotting libraries, JupyterLab, and ipykernel, which lets notebooks use this environment. Installing everything in a single command matters, because conda works out compatible versions for all the packages together instead of patching things one at a time. Always name environments after the project, so you can tell them apart later.
Worked example: a quick smoke test. Here's a quick smoke test I run on every new machine. It imports pandas and scikit-learn and prints two things. The first is sys dot executable, the full path of the Python that's actually running, which should point inside your aicourse environment. The second is the version of each library. Then it builds a tiny table of three online orders and adds up the totals, which should print seventy five point five. If you see the right path, sensible versions and that number, your environment is healthy. Save this as a file called check underscore env dot py, and run it whenever something feels off. It takes two seconds and answers the question "am I in the right place?"
Point VS Code at the environment. Next, VS Code. Install two extensions from Microsoft: Python and Jupyter. Then open the command palette with Control Shift P, or Command Shift P on a Mac, type Python select interpreter, and pick the aicourse environment from the list. From now on, VS Code uses that Python when you run scripts, and its terminal activates the environment for you. The most useful habit here is a glance at the bottom status bar, which shows the active interpreter. If it says base, or some system Python, your code is running somewhere you didn't intend. That single glance will save you from a lot of confusing "module not found" errors.
Jupyter kernels. Notebooks add one more layer, called the kernel. The kernel is the Python process that actually runs your cells, and each kernel belongs to one environment. With the aicourse environment active, run the command at the bottom of the slide: python dash m ipykernel install dash dash user dash dash name aicourse. That registers the environment once, and from then on you can choose it from the kernel picker in VS Code or in JupyterLab. One rule saves a lot of confusion: after installing a new package, restart the kernel. A kernel that's already running won't see packages that were installed after it started, just like a running service doesn't pick up a new config until you restart it.
Capture the setup in a file. Finally, write your setup down. An environment dot yml file lists the environment's name, where packages come from, and what to install. Keep it in your project folder and commit it to Git along with your code. Now anyone, on any machine, can recreate the same environment with one command: conda env create dash f environment dot yml. If you're used to infrastructure as code, this is the same idea applied to your Python setup. When you add a package, add it to the file too, and rebuild occasionally from scratch to prove the file still works. A setup that only exists on your laptop is a setup you'll eventually lose.
Common mistake: mixing pip and conda. The most common mistake I see is mixing pip and conda without thinking. Someone runs pip install with no environment active, and the package lands in base or in the system Python, where the project never sees it. Or pip quietly replaces a package that conda installed with a different build, and imports start failing in strange ways. The rule is simple. Install with conda first. Use pip only for packages that aren't available on conda-forge, and only after activating the right environment. When in doubt, run where python on Windows, or which python on Mac and Linux, and confirm the path points inside your environment folder.
Recap. Let's recap. First, give every project its own conda environment, installed from conda-forge, and keep base clean. Second, an environment only helps if your tools actually use it, so make sure the VS Code interpreter and the Jupyter kernel both point at it, and check the status bar before you run anything. Third, capture the setup in a short environment dot yml file, so you can rebuild it anywhere in minutes. With this in place, the rest of the course is about ideas and code, not about fighting installers.
Your exercise. Here's your exercise before the next lesson. Install Miniforge and create the aicourse environment with the packages from this lesson. Save the smoke test as check underscore env dot py and run it twice, once from the VS Code terminal and once from a notebook using the aicourse kernel, and confirm both print the same Python path. Then write your environment dot yml file. Finally, the real test: delete the environment completely and rebuild it from the file. If the smoke test passes again, your workstation is ready, and so are you. In the next lesson, we'll use it for a fast Python refresher focused on working with data.
تعمل لغة Python داخل متصفحك دون أي تثبيت. في أول تشغيل يُنزَّل Python (نحو 20 ميجابايت، ثم يُحفظ في المتصفح).
1 Your first smoke test
The table df holds three online orders. Replace None so that total_sales is the sum of the total column, then run your code and check that it prints 75.5.
2 Am I in the right environment?
Write in_env(executable, env_name) that returns True when the Python path executable belongs to the conda environment env_name, and False otherwise. Paths can use Windows backslashes or Linux slashes. Conda keeps environments in a folder called envs, so the environment's name is the folder right after envs.
3 Write environment.yml with code
Complete make_env_yaml(name, packages, python) so it returns the text of an environment.yml file: the name, the conda-forge channel, then under dependencies one line for Python ( - python=3.11) followed by one line per package, in order.
أجب عن كل الأسئلة ثم تحقّق. تنجح بنسبة 70%، ويمكنك إعادة المحاولة كما تشاء.
ملخص الدرس
- مشاهدة الفيديو
- التمارين
- اجتياز الاختبار (70%)
- One project, one conda environment
- VS Code and Jupyter must point at it
- environment.yml makes it rebuildable anywhere