Best practice before creating python project to avoid dependencies error across programs is to code in virtual environment.
Go to desired directory in the terminal, then type in command line:
Windows:
python -m venv virtualEnvironmentName
p/s: Shorter name is better like venv or virt. For example:
python -m venv venv
Mac:
python3 -m venv virtualEnvironmentName
A directory of virtualEnvironmentName/
should be made in the selected path.
source virtualEnvironmentName/Scripts/activate
Now, the (virtualEnvironmentName)
should be shown in the Terminal above the directory:
Once the venv activated, continue working like normal in local machine.
deactivate
The output will be: Notice the (virtualEnvironmentName)
is deactivated and not shown after the deactive
command.
pip freeze
For a newly setup venv, nothing should be returned as it just freshly built.
After installing via pip install <libraryName>
, the output should be e.g.:
At the end of the project, do this:
- Windows:
pip freeze > requirements.txt
- Mac/Linux:
pip3 freeze > requirements.txt
So that, other user can simple recreate the venv via:
- Windows:
pip install -r requirements.txt
- Max/Linux:
pip install -r requirements.txt