Deploy static assets to heroku from local machine - can't open file 'manage.py': [Errno 2] No such file or directory

前端 未结 2 1391
时光取名叫无心
时光取名叫无心 2021-01-14 06:21

I am trying deploy my assets files to heroku and I get this output in my command line interface:

(nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ her         


        
相关标签:
2条回答
  • 2021-01-14 06:41

    I will show in this answer step by step what I made for deploying my Django application in Heroku. I hope this question be useful for the others also would like to say thanks to the people that spend time helping me with this

    1. Setup my settings for my static files in heroku

    • settings configurations to Heroku

    The key of that is to be able to deploy my static files to Heroku was in my settings/base.py, acccord also to the said by ahmed and Oz123 in their responses.

    Particulary, I had an issue was in the value of PROJECT_ROOT directive. Heroku suggest that PROJECT_ROOT directive of this way:

    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
    

    I did the setup PROJECT_ROOT (I denomined BASE_DIR in my settings/base.py) as says Heroku suggested, but an error always appeared, then I took the option of leaving BASE_DIR directive as Django has by default like my PROJECT_ROOT directive in relation to the heroku configuration referenced above.

    Error I was having:

    FileNotFoundError: [Errno 2] No such file or directory: '/app/neurorehabilitation/settings/static'
    

    when I had the BASE_DIR as:

    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    

    Finally, my settings/base.py file, the content was:

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.9/howto/static-files/
    
    # This line tells Django to look for static files in a folder named static inside each of our apps.
    STATIC_URL = '/static/'
    

    As my BASE_DIR (PROJECT_ROOT in heroku ) I defined the file system path, finally my STATICFILES_DIRS directive also was compromised like Oz123 did reference me in their response. :D

    # With this configuration, Django will look for static files in a folder named static inside each app and into the neurorehabilitation/static folder
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )
    

    Then according to the deployment settings in my project (pink square in this image) my settings/production.py contains:

    # ------ *** -------------
    # For deploy to heroku
    # ------ *** -------------
    
    # Update database configuration with $DATABASE_URL.
    db_from_env = dj_database_url.config(conn_max_age=500)
    DATABASES['default'].update(db_from_env)
    
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    ALLOWED_HOSTS = ['*']
    

    Is necessary also to install the dj-database-url package in my virtual environment:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ pip install dj-database-url
    Collecting dj-database-url
    Installing collected packages: dj-database-url
    Successfully installed dj-database-url-0.4.0
    You are using pip version 8.1.0, however version 8.1.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    

    Add the dj-database-url package/dependence in the requirements/production.txt file of this way:

    dj-database-url==0.4.0
    

    This was my inconvenient for which the collectstatic command did not work when heroku deployed the application and trying to copy and processing my assets to amazon S3 where I have my static files according with my settings configuration


    In addition, I wanted to share my heroku deployment process I think this will be useful or even anecdotic

    For deploy a Django application to heroku, is necessary have in the project root the following files:

    • requirements.txt file

    I had the myproject/requirements/ folder in the root of my Django project, but the heroku deployment process does not works of this way

    Then I create in the same level or hyerarchy of requirements/ folder a requirements.txt file with the following content:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat requirements.txt 
    -r requirements/production.txt%                                                 (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    

    And my requirements/production.txt file has been stayed so:

    -r base.txt
    gunicorn==19.4.5
    dj-database-url==0.4.0
    

    The requirements/base.txt file have all dependences or packages necessary in my project.

    • runtime.txt file

    I needed Heroku install the 3.4 python version in the deployment process, then was necessary to indicate it in the runtime.txt file (created in my project root) of this way

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat runtime.txt 
    python-3.4.3
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    
    • Procfile file

    A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app.

    My Procfile has been stayed so:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ cat Procfile 
    web: gunicorn neurorehabilitation.wsgi --log-file -
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    2. Testing the access from my machine through heroku toolbet

    Is necessary install the Heroku toolbet

    which provide access to the Heroku command Line Interface which is used for managing and scaling the applications and addons, etc.

    I test the access to heroku from my machine of this way:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku login
    Enter your Heroku credentials.
    Email: botibagl@gmail.com
    Password (typing will be hidden): 
    Logged in as botibagl@gmail.com
    (nrb_dev) ➜
    

    3. Creating my heroku application

    Accord to this response, as the application to deploy is python/Django the heroku application should be create of this way:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku create test-nrb --buildpack heroku/python
    Creating test-nrb... done, stack is cedar-14
    Setting buildpack to heroku/python... done
    https://test-nrb.herokuapp.com | https://git.heroku.com/test-nrb.git
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    Then, of this way my heroku application is created in my web dashboard

    4. Setup the environment variables in my project for Heroku

    I have some environment variables in my local project used for hidden my secret keys, database user, name and password, some access to my amazon web services and s3 account and the setings used for production server (DJANGO_SETTINGS_MODULE)

    Is necessary indicate to Heroku the way in that he will know these environments variables.

    Accord to this link we will configure the vars for a deployed application. Then I've setup in heroku my environment variables of this way:

    • DJANGO_SETTINGS_MODULE

    My deployment will be guided for the settings/production.py file which inherit from settings/base.py which have all the application domain packages/dependences. Then my DJANGO_SETTINGS_MODULE variable in heroku stay so:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DJANGO_SETTINGS_MODULE=neurorehabilitation.settings.production 
    Setting config vars and restarting test-nrb... done
    DJANGO_SETTINGS_MODULE: neurorehabilitation.settings.production
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    • SECRET_KEY

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set SECRET_KEY='mysecretkey'
    Setting config vars and restarting test-nrb... done
    SECRET_KEY: mysecretkey
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    • DATABASE_NAME

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_NAME=mydatabasename
    Setting config vars and restarting test-nrb... done
    DATABASE_NAME: mydatabasename
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    • DATABASE_USER

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_USER=mydatabaseuser
    Setting config vars and restarting test-nrb... done
    DATABASE_USER: mydatabaseuser
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    • DATABASE_PASSWORD : Value in single quotes

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set DATABASE_PASSWORD='mydatabasepassword'
    Setting config vars and restarting test-nrb... done
    DATABASE_PASSWORD: mydatabasepassword
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    
    • Amazon Web Services Access Key ID : Value in single quotes

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set AWS_ACCESS_KEY_ID='my_aws_access_key_id'
    Setting config vars and restarting test-nrb... done
    AWS_ACCESS_KEY_ID: my_aws_access_key_id
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    
    • Amazon Web Services Secret Access Key : Value in single quotes

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set AWS_SECRET_ACCESS_KEY='my_aws_secret_access_key'
    Setting config vars and restarting test-nrb... done
    AWS_SECRET_ACCESS_KEY: my_aws_secret_access_key
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    • Amazon Web Services Storage Bucket name : Value in single quotes

    I execute:

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku config:set AWS_STORAGE_BUCKET_NAME='my-bucket-name-in-s3' 
    Setting config vars and restarting test-nrb... done
    AWS_STORAGE_BUCKET_NAME: my-bucket-name-in-s3
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    Then according to above options in my web dashboard the environment variables conatins:

    5. Execute commit operation to the heroku repository

    First we check the files to be committed

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   requirements.txt
        new file:   runtime.txt
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
        modified:   neurorehabilitation/settings/base.py
        modified:   neurorehabilitation/settings/development.py
        modified:   neurorehabilitation/settings/production.py
        modified:   requirements.txt
        modified:   runtime.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        .idea/
        Procfile
        gunicorn_start
    
    (nrb_dev) ➜  neurorehabilitation_projects git:(master)
    
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add requirements.txt 
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add runtime.txt     
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add Procfile 
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/base.py 
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/development.py
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git add neurorehabilitation/settings/production.py
    

    Make the commit operation

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git commit -m 'Setup changes and configurations for deploy to heroku'
    [master 69d69fe] Setup changes and configurations for deploy to heroku
     6 files changed, 38 insertions(+), 5 deletions(-)
     create mode 100644 Procfile
     create mode 100644 requirements.txt
     create mode 100644 runtime.txt
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    

    6. Deploy our code application to heroku repository

    Before, I check my remotes repos. I have reference the heroku alias

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git remote -v
    heroku  https://git.heroku.com/neurorehabilitation.git (fetch)
    heroku  https://git.heroku.com/neurorehabilitation.git (push)
    origin  https://bgarcial@bitbucket.org/bgarcial/neurorehabilitation_projects.git (fetch)
    origin  https://bgarcial@bitbucket.org/bgarcial/neurorehabilitation_projects.git (push)
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗
    

    Deploy to heroku repository

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ git push heroku master 
    Counting objects: 20, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (13/13), done.
    Writing objects: 100% (13/13), 1.17 KiB | 0 bytes/s, done.
    Total 13 (delta 11), reused 0 (delta 0)
    remote: Compressing source files... done.
    remote: Building source:
    remote: 
    remote: -----> Using set buildpack heroku/python
    remote: -----> Python app detected
    remote:      $ pip install -r requirements.txt
    remote: 
    remote:      $ python manage.py collectstatic --noinput
    remote:        Found another file with the destination path 'admin/css/forms.css'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
    remote:        147 static files copied.
    remote: 
    remote: 
    remote: -----> Discovering process types
    remote:        Procfile declares types -> web
    remote: 
    remote: -----> Compressing...
    remote:        Done: 46.1M
    remote: -----> Launching...
    remote:        Released v15
    remote:        https://test-nrb.herokuapp.com/ deployed to Heroku
    remote: 
    remote: Verifying deploy.... done.
    To https://git.heroku.com/test-nrb.git
       9f0d32f..fcc62c9  master -> master
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    7. Execute the migrations process

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python manage.py migrate
    Running python manage.py migrate on test-nrb.... up, run.1678
    System check identified some issues:
    
    WARNINGS:
    medical_encounter_information.RehabilitationSession.date_session_begin: (fields.W161) Fixed default value provided.
            HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
    medical_encounter_information.RehabilitationSession.date_session_end: (fields.W161) Fixed default value provided.
            HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
    userprofiles.User.birth_date: (fields.W161) Fixed default value provided.
            HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
    Operations to perform:
      Apply all migrations: medical_encounter_information, userprofiles, contenttypes, auth, admin, sessions
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0001_initial... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying userprofiles.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying userprofiles.0002_auto_20160225_2130... OK
      Applying userprofiles.0003_auto_20160225_2130... OK
      Applying medical_encounter_information.0001_initial... OK
      Applying medical_encounter_information.0002_auto_20160225_2130... OK
      Applying medical_encounter_information.0003_auto_20160225_2130... OK
      Applying medical_encounter_information.0004_auto_20160225_2211... OK
      Applying medical_encounter_information.0005_auto_20160225_2211... OK
      Applying medical_encounter_information.0006_auto_20160225_2303... OK
      Applying medical_encounter_information.0007_auto_20160229_2204... OK
      Applying medical_encounter_information.0008_auto_20160229_2208... OK
      Applying medical_encounter_information.0009_auto_20160301_0130... OK
      Applying medical_encounter_information.0010_auto_20160301_0312... OK
      Applying medical_encounter_information.0011_auto_20160301_1525... OK
      Applying medical_encounter_information.0012_auto_20160301_1601... OK
      Applying medical_encounter_information.0013_auto_20160301_1606... OK
      Applying medical_encounter_information.0014_auto_20160301_1629... OK
      Applying medical_encounter_information.0015_auto_20160301_1633... OK
      Applying medical_encounter_information.0016_auto_20160301_1636... OK
      Applying sessions.0001_initial... OK
      Applying userprofiles.0004_auto_20160225_2211... OK
      Applying userprofiles.0005_auto_20160225_2211... OK
      Applying userprofiles.0006_auto_20160225_2303... OK
      Applying userprofiles.0007_auto_20160229_2204... OK
      Applying userprofiles.0008_auto_20160229_2208... OK
      Applying userprofiles.0009_auto_20160301_0130... OK
      Applying userprofiles.0010_auto_20160301_0312... OK
      Applying userprofiles.0011_auto_20160301_1525... OK
      Applying userprofiles.0012_auto_20160301_1601... OK
      Applying userprofiles.0013_auto_20160301_1606... OK
      Applying userprofiles.0014_auto_20160301_1629... OK
      Applying userprofiles.0015_auto_20160301_1633... OK
      Applying userprofiles.0016_auto_20160301_1636... OK
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    8. I create my super user in my application deployed in the platform

    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ heroku run python manage.py createsuperuser
    Running python manage.py createsuperuser on test-nrb.... up, run.7499
    Username: bgarcial
    Email address: bgarcial@eafit.edu.co
    Password: 
    Password (again): 
    Superuser created successfully.
    (nrb_dev) ➜  neurorehabilitation_projects git:(master) ✗ 
    

    And That's all. With the previous process we can deploy on heroky a Django application

    0 讨论(0)
  • 2021-01-14 06:52

    Have you properly defined:

    STATICFILES_DIRS
    

    Inside your settings.py?

    Have you created it locally and added it to git (as empty directory...). The trace you have indicates that this folder is missing. I have created a simple project with the following settings:

    ~/Software/h/hrku $ tail hrku/settings.py 
    USE_TZ = True
    
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.9/howto/static-files/
    
    STATIC_URL = '/static/'
    
    STATIC_ROOT = '/var/www/dj'
    STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__), '..', 'static'),)
    

    As you can see the STATICFILES_DIRS contains only one directory, which does not exist:

    ~/Software/h/hrku $ ls -l /home/ozn/Software/h/hrku/static
    ls: cannot access /home/ozn/Software/h/hrku/static: No such file or directory
    

    The command collectstatic fails:

    ~/Software/h/hrku $ python manage.py collectstatic
    
    You have requested to collect static files at the destination
    location as specified in your settings:
    
        /var/www/dj
    
    This will overwrite existing files!
    Are you sure you want to do this?
    
    Type 'yes' to continue, or 'no' to cancel: yes
    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
        utility.execute()
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
        output = self.handle(*args, **options)
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
        collected = self.collect()
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
        for path, storage in finder.list(self.ignore_patterns):
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
        for path in utils.get_files(storage, ignore_patterns):
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
        directories, files = storage.listdir(location)
      File "/home/ozn/.virtualenvs/h/lib/python3.5/site-packages/django/core/files/storage.py", line 299, in listdir
        for entry in os.listdir(path):
    FileNotFoundError: [Errno 2] No such file or directory: '/home/ozn/Software/h/hrku/static'
    

    I hope this will lead you to solving your problem.

    0 讨论(0)
提交回复
热议问题