Search code examples
djangovagrantpycharm

How can I get PyCharm to use the Vagrant Directories rather than my development machines?


When I try to run my Django app via PyCharm inside of Vagrant, it sends my Windows Path over SSH which then provides an error in the shell:

> ssh://[email protected]:2200/usr/bin/python -u C:/Projects/dev_project/dev/manage.py runserver 0.0.0.0:8000

bash: line 0: cd: C:/Projects/dev_project/dev: No such file or directory /usr/bin/python: can't open file 'C:/Projects/dev_project/dev/manage.py': [Errno 2] No such file or directory

I created a Python Project in PyCharm (and instantiated a Django Project in a sub-folder) in order to incorporate Vagrant.

dev_project (PyCharm project root)
  |--.vagrant
  |--dev (Django project root)
       |--dev
       |--app
       |--manage.py
  |--Vagrantfile

In Settings

I enabled Django Support (providing the Windows Paths as there is no other option) in Settings > Languages & Frameworks > Django. The Vagrant Python Interpreter is selected as the Project Interpreter (and Django Console & Python Console)

In Run Configurations

My host is 0.0.0.0 and port is 8000 My Python Interpreter is the Vagrant Environment. I am also adding the Content Roots & Source Roots to the Python Path.

The bizarre problem is it was working fine, and then I exited out and it broke again. Also, I do not have 'Working Directory' explicitly defined anywhere.


Solution

  • This blog article showed me my issue, as I did not have my Path Mappings setup within my Run Configuration.

    From the Article (Configure Your Project to Use the Correct Interpreter):

    • Select Run->Edit Configurations
    • Select the configuration on the right PyCharm created for us. In my case it was called “session_tracker”.
    • Change “Python interpreter” to our newly created one.
    • A new field will appear. Click the button next to “Path mappings”. A new window will appear to let you create your mappings.
    • Vagrant shares a local directory with the VM. My Vagrant file is configured so a folder is called “django_shared” in our [VagrantFolder] locally, and “django_shared” in the home directory on the VM are the same. You need to enter the full paths of each of those in the “Edit Path Mappings” window. - Click the + button to create a new mapping, then enter the values for the mapping on each side.