6

I have set up already my configuration so that it will run the server remotely. When I click run, I see the command used:

ssh://vagrant@localhost:2222/usr/bin/python -u "C:/Users/MyName/ProjectName/config/manage.py" runserver localhost:8080

(I've replaced the directory names for anonymity reasons).

When I do run this, It fails (obviously) because it's using a windows path to my manage.py Specifically the error I get is

`/usr/bin/python: can't open file 'C:/Users/MyName/judgeapps/config/manage.py': [Errno 2] No such file or directory

What I can't figure out after extensive googling, is how to force django to use a path on my vagrant machine. How can I go about doing this?

1 Answer 1

12

The trick is creating a Python interpreter in PyCharm, and configuring the project to use this interpreter.

Note: The following applies to PyCharm Professional 4.0.

Create a Python Interpreter for Vagrant

  1. Start your Vagrant machine from PyCharm by navigating to Tools->Vagrant->Up
  2. SSH into your Vagrant box: Tools->Start SSH Session. Select Vagrant at [VagrantFolder] from the list that appears.
  3. From the terminal that appears, run which python. This will give you an absolute path to python on your virtual machine.
  4. File->Settings->Project->Project Interpreter. Click the + button to create a new one.
  5. Choose Vagrant. Your Vagrant instance folder should be the location of your VagrantFile on your host machine. Python interpreter path should be set to the absolute path you found in step 3 above.
  6. Click OK to save. Note: Vagrant has to be up in order for this to work.

Configure Your Project to Use the Correct Interpreter

  1. From the Run menu, select Edit Configurations
  2. Click + and add a new Django Server
  3. Set your Host to 0.0.0.0. This will bind the runserver command to an external IP.
  4. Check Run browser and set the URL to the host/port you mapped to your VM in your VagrantFile (for example, if I map my host's port 8080 to Vagrant's 8000, I'd use http://127.0.0.1:8080/)
  5. Choose the Python interpreter that you set up in the above section from the Python interpreter dropdown
  6. Add your absolute path mappings (this is sometimes optional, depending on where your VagrantFile is stored).
  7. Click OK to save.

Run your project, and enjoy the glory that is Vagrant.

7
  • Thanks, but for some reason I can't actually access the path mappings tab. Any suggestions? Edit: I do have a remote interpreter set up as my default and chosen here.
    – Kunc
    Nov 27, 2014 at 9:45
  • Clicking on the ... button doesn't pop up a new window? The input itself should be disabled, but there's a button to the right of it.
    – rnevius
    Nov 27, 2014 at 9:54
  • The Input is disabled, and clicking on the '...' does nothing. Shift + enter also fails.
    – Kunc
    Nov 27, 2014 at 10:02
  • 1
    Looking over your edited tutorial, I see that I didn't select vagrant as the option for my interpreter. I did this as I got a File not found exception for my private key (it's there, just not being found). Could this be the root of my problem?
    – Kunc
    Nov 27, 2014 at 10:05
  • That could definitely be the issue. If you vagrant ssh from PyCharm, do you get the same error?
    – rnevius
    Nov 27, 2014 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.