Search code examples
pythonwindowsjupyter-notebookpython-venv

Correct Order of Steps Starting New Python Project on Windows


I've been eyeballs deep in pip, Jupyter, python documentation trying to understand how these components interact in a single project on the Windows OS.

  1. Where in the folder tree and when do I tutorial_env\Scripts\activate?
  2. Where in the folder tree and when do I pip install [package name]?
  3. Where in the folder tree and when do I jupyter notebook?

My current folder tree has tutorial_env as the parent with the Lib folder and my main project folder as siblings within the virtual environment. I've pip install [package name] for all of my packages and can see those files in the Lib folder. When I import them into my main.py file within the main project folder, the first import statement gives a ModuleNotFoundError: No module named 'numpy'.

Seems reasonable that I need to rearrange my files. How do the pros do it?


Solution

  • If your getting ModuleNotFoundError: No module named 'numpy' , it typically indicates that Python is not looking in the correct location for the installed modules or you haven't installed them. This can happen if the virtual environment in which the packages were installed is not activated, or if you're inadvertently using a different Python interpreter when your trying to run your code. To check that the environment is activated in the script your running you can look at the command prompt prefix or use where python in the terminal. This command should give you the path tot he python interpreter your using.

    Going into your questions all of those commands should be run from the root/parent directory tutorial_env. First got into parent directory it would look like this from the command prompt. Replace Desktop\...\tutorial_env with the path to the parent folder.

    cd Desktop\...\tutorial_env
    

    Then create a virtual environment. I'm assuming you have already done this.With env being the name of the virtual environment.

    python -m venv env
    

    Activate the environment.

    env\Scripts\activate
    

    Then you can pip install your dependencies/libraries into that virtual environment.

    The folder structure might look something lie this:

    tutorial_env/
    ├── env/                   # Virtual environment
    ├── src/                   # Source files like Python scripts
    │   └── main.py
    ├── notebooks/             # Jupyter notebooks
        └── analysis.ipynb
    

    The Lib directory you mentioned I dont think should be needed, the python environment should automatically created and manage the installed libraries/dependencies itself. Just ensure you're working within the activated environment.