Skip to content Skip to sidebar Skip to footer

Relocating Virtual Environment Project From Local To Server (flask Project), Have To Install Dependencies Again?

I have created a flask application in a virtual environment on my local machine and I could run it locally (at http://localhost:5000). I then put this project in a repo and I then

Solution 1:

As a general rule python environments are not portable across machines.

This means that you cannot reliably expect to port the virtual environment across machines. This is especially true if you are moving stuff between different operating systems. For example, a virtual environment created in Windows will not work in Linux.

Similarly, a virtual environment created in OSX will not work in Linux. Sometimes, you can get Linux > Linux compatibility, but this is by chance and not to be relied upon.

The reasons are numerous - some libraries need to be built against native extensions, others require compatible system libraries in place to work, etc.

So, the most reliable workflow is the following:

  1. You can (but I would recommend against this) put your virtual environment in the same directory as your project. If you do so, make sure you don't add the virtual environment root directory to your source control system. It is best to separate your virtual environments from your source code (see the virtualenvwrapper project project for a great way to manage your virtual environments separately).

  2. You should create a requirements file, by running pip freeze > requirements.txt. Keep this file updated and add it to your source control system. In your target system, simply create an empty virtual environment and then pip install -r requirements.txt to make sure all requirements are installed correctly. Doing so will make sure that any native extensions are also built and installed.


Solution 2:

A few possible issues:

  • When you created your original virtual environment did you specify --no-site-packages if not your package may be using elements from the system.
  • Some packages rely on system installed libraries that may be missing on your target system
  • Is your server running on a similar set of hardware to your development system with the same OS - if not your virtualenv is likely not to work without re-installing packages as any C/C++ extensions will have been built for the wrong hardware/OS and will not work.

The thing is that virtualenv is not a package builder, (look at pyinstaller for that), but rather a development and test environment when you go to distribute your code to a new platform then, provided you started off with --no-site-packages you can easily find out which packages you need to find out what you need to install on the new target.

So basically - Yes you, or more likely the system admin, does need to run pip install flask and probably several other things!


Post a Comment for "Relocating Virtual Environment Project From Local To Server (flask Project), Have To Install Dependencies Again?"