Run a Python script in Vagrant

florvdl

I'm learning how to use Vagrant with a Udacity course, and we're asked to run a Python script database_setup.py in our virtual machine.

For this, I created a folder "udacityproject" inside my vagrant folder on my computer. I saved my file database_setup.py there.

Now on Bash, I do

$ vagrant up
$ vagrant ssh
$ cd udacityproject 
$ python database_setup.py

The interface returns:

"python: can't open file 'database_setup.py': [Errno 2] No such file or directory".

It must be a silly mistake, but I cannot see what I am doing wrong... A similar topic was opened here (Run Python script in Vagrant) but the answers are not helping me.

qvpham

The vagrant folder on your computer, which contains the file VagrantFile, is the folder /vagrant on your vm (It is under /). It's not your home directory. After vagrant ssh you are logged in home directory of user vagrant. It's /home/vagrant/.

$ vagrant ssh
$ pwd
/home/vagrant

The tree looks like that:

/root
/tmp
/usr
/var
/home
     |-- vagrant  # <-- You are here after logging
/vagrant
        |-- udacityproject
                         |-- database_setup.py  # <-- Your script is here
...

To run your script you must go to /vagrant

$ cd /vagrant

With ls * you can check if your file exists. Now go to your created folder and run your script

$ cd udacityproject
$ python database_setup.py

Or simply do that from beginning:

$ vagrant ssh
$ python /vagrant/udacityproject/database_setup.py

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related