Skip to content

How do I run a py file inside a virtual environment on Windows 7?

I need help understanding venv and project management on a Windows 7 system.

I have installed Python 3.7.4. Running the command ‘pip list’ results in the following:

Package     Version
pip         19.0.3
setuptools  40.8.0

Under the path C:ProjectsNeuralNetworks I have the following files/folders:

project_env #virtual environment created with venv

When activating my virtual environment ‘project_envScriptsactivate.bat’ the command ‘pip list’ results in:

Package     Version
numpy       1.16.4
pip         19.0.3
setuptools  40.8.0

When I go to run I get the error “NameError: name ‘numpy’ is not defined”.
import numpy as np
input('Press enter to continue...')

I expect to see the version of numpy but instead I get the error that numpy is not found which tells me I don’t really understand how these directories work.

Solution: I added runserver.bat to my project directory with following.

CALL C:ProjectsNeuralNetworksproject_envScriptsactivate.bat
python runserver

I run the runserver.bat and everything works perfectly.


You need to create a .bat file where you added some code inside to activate the environment first and then run your python file.

something like :

@echo off
cmd /k "cd /d ..envScripts & activate & cd /d    ..foldername & python

Reference : A Python script that activates the virtualenv and then runs another Python script?