i am just starting a new project on my pycharm i start a new project and run my server but am getting this error message couldn't import django, Must i install django for every project am creating?
If you are using virtual environment for every new project then you need to install django each time. Other wise you can use single virtual environment for all the project. It depends on you how you prefer. But I highly recommend you to use separate virtual environment for new project.
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
Does every Django project needs a separate virtual environment? Can't I use one of my exciting virtual env ? If I can't, then what's the problem?
I'm getting ready to move my Django project from my laptop to my server. What is the recommended way to do this? E.g., is there a Django command that will package everything up (and select the correct settings file for test vs prod servers) and create a zip or tar file that can be moved over to the server? Sort of like Ant for building Java projects.
I recommend using virtual environment for your Django project.
source bin/activate your virtual environment in your server would simulate the same setup as of your local.
In your project
List all your dependencies in requirements.txt and settings in my_settings.py apart from django settings.py
In your server
just pull/transfer your code via git or any other means and activate virtual environment.
pip install -r reuirements.txt and change any minor changes required in my_settings
Take care of your migrations and Db setup. You may have to run migrations if you are migrating to your server for the first time.
And thats it you are up and running.
I've been running my Django project (website) in my local virtual environment. However I now want to go live and make the website public - so I've created my remote server on Digital Ocean, and have been following it's tutorial on settings up Django. However I've reached this point in the tutorial where it says to create a virtual environment. I thought virtual environments were only used for testing your application offline? Why do I need a virtual environment for my application running on a remote server?
I'm not really familiar with Digital Ocean, but creating a virtualenv should not be strictly required.
It is still a good idea and I'd recommend doing it though.
It will prevent packages required for your application that you install to your virtual environment from messing up other Python applications requirements.
For example, let's say your application requires coolpackage 0.9.12, and another application requires coolpackage 1.1.6. If there are breaking changes going from 0.9.12 to 1.1.6, installing coolpackage 1.1.6 will break your first application.
Also, keep in mind that some functionality of your operating system might require specific versions of Python packages (which might even have been installed by some other means than pip) that you could break in this way.
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.