Issue
Sorry if this was answered in another post.
I am very new to python and learning about the virtual environment. I understand that I am supposed to have all the libraries installed in the virtual environment and create the requirement.txt, so others can install using that. However, I am not sure what the best practice to deploy to production?
The reason I ask is that no one supposes to have access to the production environment, the deployment is through a predefined pipeline, and my understanding is that it will zip all my code and deploy it to production, no one suppose to go into production to do any manual work. I can try to get the pipeline to run a script to install all the libraries base on the requirement.txt, but I am not sure if the firewall setting is the same. Should I package the libraries as well?
also, how should I trigger the python script? should I have a wrapper script to activate the vevn before calling python script and deactivate it after? or there is a easier way?
Solution
First, there are several reasons for virtual environments. The primary reason is that you may have multiple applications with different and conflicting dependencies (i.e. requiring different versions of the same modules). A secondary benefit is that it does neatly "enumerate" the required modules for your application by executing a pip freeze
command in your virtual environment. This can be useful whether you are planning to move to a new environment or not.
As long as your application has a shebang as follows:
#!/full-path-to-the-bin-directory-of-my-virtual-environment/python3
then you do not have to activate the virtual environment for your script to find the environment's modules. Anyway, this is my experience with virtual environments created with the python -m venv
command.
So, ideally you would have shell access to your production environment and be able to create the virtual environment there and initialize it with a requirements.txt
file created from using a pip freeze
command in your development virtual environment. I would, therefore, not put the virtual environment under source control but would put the requirements.txt
file under source countrol.
Answered By - Booboo Answer Checked By - Terry (WPSolving Volunteer)