简体   繁体   English

无法在Vagrant中使用Virtualenv导入模块

[英]Cannot import modules using Virtualenv in Vagrant

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. 这是我第一次尝试建立一个流浪汉环境或python virtlenv,所以如果我遗漏了一些基本的东西,请原谅我。

Right now, I ssh into my vagrant box and in the home directory I have placed my venv folder. 现在,我进入我的流浪者盒子,在主目录中我放置了我的venv文件夹。 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: 从我的主目录,我移动到/ vagrant,在这里,我的项目文件布局如下:

├──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: 例如,如果我在/vagrant/project/project/target/test.py并尝试:

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 如果我在/ vagrant / project / project目录中运行

import exceptions

that works fine. 工作正常。

I have read up on similar problems people have experienced on StackOverflow. 我已经了解了人们在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) 基于这个问题: 无法从virtualenv导入包我已检查过我的sys.executable路径在我的python解释器中以及运行脚本时都是相同的(home / vagrant / venv / bin / python)

Based on this question: Import error with virtualenv . 基于这个问题: 使用virtualenv导入错误 I have run ~/venv/bin/python directly and attempted to import, but the import still fails. 我已直接运行〜/ venv / bin / python并尝试导入,但导入仍然失败。

Let me know if there is more information I can provide. 如果我能提供更多信息,请告诉我。 Thank you. 谢谢。

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 . 您可以通过编写setup.py文件并调用python setup.py install将项目安装到虚拟环境中。 See the Python Packaging User Guide . 请参阅Python Packaging用户指南

  2. You can set the PYTHONPATH environment variable to point to your project, like this: 您可以将PYTHONPATH环境变量设置为指向项目,如下所示:

     $ export PYTHONPATH=$PYTHONPATH:/vagrant/project 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM