4

This is my first time trying to set up a vagrant environment or a python virtuelenv, so forgive me if I am missing something basic.

Right now, I ssh into my vagrant box and in the home directory I have placed my venv folder. I have run

source venv/bin/activate 

From my home directory I move to /vagrant, and within here I have my project files laid out something like this:

├──project
├── LICENSE
│
├── project
│   │   ├── exceptions.py
│   │   ├── __init__.py
│   │   ├── resources
│   │   │   ├── base.py
│   │   │   ├── __init__.py
│   │   └── target
│   │       └── __init__.py
│   │       └── test.py
│   ├── README.md

My problem is I am unable to import my modules in different directories. For example, if I am in /vagrant/project/project/target/test.py and I attempt:

import project.exceptions

I will get the error

ImportError: No module named project.exceptions

If I am in the /vagrant/project/project directory and I run

import exceptions

that works fine.

I have read up on similar problems people have experienced on StackOverflow.

Based on this question: Can't import package from virtualenv I have checked that my sys.executable path is the same in both my python interpreter as well as when I run a script (home/vagrant/venv/bin/python)

Based on this question: Import error with virtualenv. I have run ~/venv/bin/python directly and attempted to import, but the import still fails.

Let me know if there is more information I can provide. Thank you.

Community
  • 1
  • 1
roach
  • 73
  • 7

1 Answers1

0

You have two options:

  1. You can install your project into the virtual environment, by writing a setup.py file and by calling python setup.py install. See the Python Packaging User Guide.

  2. You can set the PYTHONPATH environment variable to point to your project, like this:

    $ export PYTHONPATH=$PYTHONPATH:/vagrant/project
    
William
  • 2,695
  • 1
  • 21
  • 33