Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Django project with .pyc files without using source codes?

I have a django project and i want to create the .pyc files and remove the source code. My project folder name is mysite and I ran the command python -m compileall mysite. The .pyc files are created. After that i tried to run my project with python __pycache__/manage.cpython-37.pyc runserver command but i've got an error such as ModuleNotFoundError: No module named 'mysite'

There are two things I would like to ask about this. First, how can I solve this problem and run my project with .pyc file? Secondly, is it enough to move the .pyc files created to a separate folder in accordance with the django project structure?

like image 674
Aslı Kök Avatar asked Jun 21 '20 12:06

Aslı Kök


People also ask

Can django work without database?

However, while you can use django with no database, the object-relational mapper is pretty much its first and foremost advertised feature. Django was designed to produce database-backed web sites, so if you're not going to use a database you might end up dealing with a bunch of unnecessary hassle.

How to create a Django project?

To create a Django project, you will have to start your virtual environment which we set up in the last tutorial of Install Django and Set up a virtual environment, in that directory, you will be creating your Django project. This command will install Django in your virtual environment and will make it error free.

How to create a virtual environment in PyCharm for Django?

Then, select the desired project type (here it is Django). Specify the project name and location. Python best practice is to create a virtualenv for each project. In most cases, PyCharm create a new virtual environment automatically and you don't need to configure anything. Still, you can preview and modify the venv options.

What is mysite settings Py in Django?

mysite/settings.py: This file contains configuration for your Django project. mysite/urls.py: This file contains the URL declarations for your Django project. mysite/wsgi.py: This file defines an entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

Is Django a good choice for beginners?

Though you can experiment with Django for beginner-level projects for learning purposes, Django isn’t suitable for basic apps as it is pretty complex. If your app doesn’t need backend development, then Django would be an unnecessary addition to your technology stack.


1 Answers

First of all, I created a new folder in another directory such as a new django project and i created my app folders, static folder, templates folder etc. manually as the same as my django project architecture that I created before.

Then, I moved the .pyc files that I created with the compileall command to my new project folders.

As you know, while creating .pyc files, a .cpython-37 section is added to the file names automatically (for example, manage.py -> manage.cpython-37.pyc). I removed that section and i converted them to manage.pyc, views.pyc, etc.

So my file structure was like this:

mysite/
    manage.pyc
    mysite/
        __init__.pyc
        settings.pyc
        urls.pyc
        wsgi.pyc
    app/
        migrations/
                __init__.pyc
        __init__.pyc
        admin.pyc
        apps.pyc
        models.pyc
        tests.pyc
        views.pyc

        ...

After I created this django project structure with .pyc files, i ran the python manage.pyc runserver command and it works.

like image 160
Aslı Kök Avatar answered Oct 05 '22 00:10

Aslı Kök