1

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?

1
  • 1
    Too many questions in one. You should ask your questions separately. Also maybe read How to Ask.
    – sinoroc
    Commented Jul 5, 2020 at 16:49

3 Answers 3

7

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.

2

For your program to work anywhere be it production or development, it needs the required library. Generally what is done is, we create a new virtual environment (like Booboo said with 'python -m venv <env_name>'), on the server and install all the required packages (pip install -r requirements.txt) ('requirements.txt' should be created on your development machine with 'pip freeze > requirements.txt')

Since you say, no one is allowed to access the production server directly, there must be some step, your system administrator wants you to follow. Only he/she can help you in that case. As for firewall, if your program serves to a certain port and if your program must be accessible from other computers apart from the localhost, than that particular port must be opened. That is something your server administrator shall do for you.

0

When it comes to deploying applications on servers without internet. Consider deploying using PEX, Pants build system. Or as Booboo says you can just deploy and run using virtual environments(not really pythonic way) but it works. For our use case where i am we are actually do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.