I have a backend app based on Node.js. The code is writen on JavaScript, except on folders 'scripts' which it is on Python.
I have some external libraries installed (pandas, matplotlib...) to execute those scripts and I used the virtual env (venv) to use them it correctly.
However, I always need to activate 'source venv/bin/activate' before to execute them (when I am in localhost).
The problem is in production.
Is there any way to let them activate permanently on production? Or other extra software? I hosted these files on VPS in Hostinger, but on production I got some errors in scripts which has some libraries installed.
This 'activate' might be the problem.
To make the comments an answer:
In production, you'll be running your app proper with a service manager such as systemd (that makes sure it stays running). You can direct the service manager to directly use the venv's Python, e.g. /home/app/venv/bin/python myapp.py; you don't need the activate script.
To have the virtualenv automatically activated for ad-hoc use on the production server, you can use a .bashrc file, e.g. /home/app/.bashrc that includes source ~/venv/bin/activate.
Related
Python web development newbie question here. I'm coming from PHP/Laravel and there you have Homestead which is a pre-configured Vagrant box for local development. In a so-called Homestead file, you configure everything such as webserver, database or PHP version. Are there any similar pre-configured dev environments for Django?
I already googled and there don't seem to be any official or widely-used Vagrant boxes for Django. The official Django tutorial even tells you how to install and set up Apache and your preferred database. This is a lot of work everytime you want to create a new Django project, especially if those projects run in different production environments. All the other tutorials I've found just explain how you set up virtual environments with venv or the like. But that doesn't seem to be sufficient to me. What you obviously want is a dev environment that is as close as possible to your production environment, so you need some kind of virtual machines.
I'm a little bit confused right now. Do you just grab some plain Ubuntu (or any other OS) Vagrant box and install everything yourself? Don't you use Vagrant at all but something else? Did I miss something and the Python web development workflow is completely different?
The typical local development in Django just uses the builtin web server and an SQLite database. The steps to get that up and running are:
Ensure you have the desired version of Python installed.
Create a virtual env to isolate libraries needed for your project from the rest of the system (this is optional by highly recommended, I'd actually recommend using Poetry).
Install Django, probably via pip.
Run manage.py runserver (and migrate the database and set up a superuser, yada yada).
That's pretty much it and sufficient for local development. What you need to be aware of is that some differences exist between SQLite and Postgres, MySQL etc., and if you hit the spots where the difference is important, you'll want to set up your targeted database as well to develop directly against it. That can probably happen in a Docker container if that makes sense for you. But there's little reason to put Django into a container during development, unless your project is especially complex and requires simulating certain conditions which the builtin server somehow can't.
Does this help?
$ python3 -m venv my_env # create your virtual environment
$ source my_env/bin/activate # Any package you install will be inside this environment
$ pip install -r requirements.txt # can also install packages indivdually
$ deactivate # get out of the isolated environment
Here's the doc
I am working through the process of installing and configuring the Superset application. (A Flask app that allows real-time slicing and analysis of business data.)
When it comes to the Python virtual environment, I have read a number of articles and how-to guides and understand the concept of how it allows you to install packages into the virtual environment to keep things neatly contained for my application.
Now that I am preparing this application for (internal) production use, do I need to be activating the virtual environment before launching gunicorn in my upstart script? Or is the virtual environment more just for development and installing/updating packages for my application? (In which case I can just launch gunicorn without the extra step of activating the virtualenv.)
You should activate a virtualenv on the production server the same way as you do on the development machine. It allows you to run multiple Python applications on the same machine in a controlled environment. No need to worry that an update of packages in one virtualenv will cause an issue in the other one.
If I may suggest something. I really enjoy using virtualenvwrapper to simplify the use of virtualenvs even more. It allows you to define hooks, e.g.: preactivate, postactivate, predeactivate and postdeactivate using the scripts in $VIRTUAL_ENV/bin/. It's a good place for setting up environmental variables that your Python application can utilize.
And a good and simple tool for process control is supervisord.
I have an application using Django1.9, and python2.7. I recently flushed my PostgreSQL database on my production server, and now whenever I try to use the application it is telling me there are missing modules. I never faced this issue before so I am curious, when you put your application on a production server, does your virtual environment go with it ? If so, does flushing your database have any effect on your virtual environment ?
I have been getting past the issues by downloading each module to a third parties directory in my application, and including them in my 'installed apps' list in my setting file, but I wouldn't want to continue doing that if there are 100+ modules I need to download.
I also tried to use pip install on my production server, and it said that the command was not found, although I have the latest version of pip installed on my mac ?
I am curious, when you put your application on a production server, does your virtual environment go with it ?
Not necessarily unless you copied the virtualenv folder with it which isn't really a good practice, you should create the virtualenv on the production server
If so, does flushing your database have any effect on your virtual environment ?
No, the database and virtualenv are completely separate
I wouldn't want to continue doing that if there are 100+ modules
Use a requirements.txt file and install them all at one go with pip install -r requirements.txt
I also tried to use pip install on my production server, and it said that the command was not found
You have to install pip first, on the production server
I have 3 standalone python applications in python Virtual env in mog_wsgi modein same server. I installed newrelic in 1st application python virtual environment and its showing fine in newrelic GUI page.
when i followed and installed same thing for 2nd and 3rd application in respective python Virtual env. but these 2 applications are not showing in Newrelic GUI application page.
For all 3 applications log file is not updating from starting.
Please help me out to configure and integrate multiple python apps which are hosted in a single server python env.
below are steps that i have followed.
centos OS
Install the New Relic Python agent in apps virtual env
source <virtual path>
pip install newrelic
pip freeze
Generate config file:
cd /etc/newrelic/
newrelic-admin generate-config <Licence Key> newrelic.ini
Validate the conf file:
newrelic-admin validate-config newrelic.ini
Configure the variables in conf file
logfile = log file name
loglevel=info
app_name = name
Integrate the appliactions mod_wsgi file with NewRelic:
Adding below in wsgi.py file
vi wsgi.py
import newrelic.agent
newrelic.agent.initialize('/etc/newrelic/newrelic.ini')
installed newrelic version is "newrelic==2.12.0.10"
Please help me.
Thank you,
subhani466#gmail.com
I know that you have posted this a while ago, but the solution I've found was to install newrelic pip install newrelic outside of virtual envs.
Anyone that is facing this issue just install new relic outside the virtual envs.
I'm a little unclear on your setup. If you have all three Python apps in the same virtual environment and you're using newrelic.ini for all three, then all three applications will report to the same UI listing. You need to either name the config files differently (newrelic.ini, newrelic2.ini, newrelic3.ini) and reference them likewise, or split the applications into separate virtual environments.
As for the log files not writing, it sounds like you don't have user rights to that directory. You can read more about this in the New Relic docs, here: https://docs.newrelic.com/docs/python/python-agent-logging
The path provided for 'log_file' should be writable to the user that
your application runs as. If using Apache/mod_wsgi that would usually
be the Apache user which has restricted access to the filesystem. You
might therefore need to create a special directory into which the log
file can be placed which is writable to the Apache user. Because the
current working directory of an application could be anything, it is
recommended that an absolute path and not a relative path be used.
If you need more help, please open a ticket with us at http://support.newrelic.com
It looks like you are using one config file for three applications.
They all have the same app_name.
Therefore, you have one APM displayed in the UI.
Remove the app_name line from newrelic.ini and pass a unique variable NEW_RELIC_APP_NAME to each app.
How can I deploy and host multiple python projects with different dependancies on the same server at the same time?
It's not true of course that only one virtualenv can be activated at once. Yes, only one can be active in a shell session at once, but your sites are not deployed via shell sessions. Each WSGI process, for example, will create its own environment: so all you need to do is to ensure that each wsgi script activates the correct virtualenv, as is (in the case of mod_wsgi at least) well documented.
Use virtualenv for python. You can can install any other version of python/packages in it, if required.