简体   繁体   English

VirtualEnv python 导入不能从项目文件夹工作,只有 virtualenv 文件夹

[英]VirtualEnv python imports not working from project folder, only virtualenv folder

I'm having trouble with virtualenv when trying to import modules installed via pip in my project directory (one up from the virtual env. directory).我在尝试导入通过 pip 在我的项目目录中安装的模块时遇到了 virtualenv 问题(从虚拟 env. 目录开始)。

my project structure is;我的项目结构是;

\Project
..\dev    (virtual env)
..test.py

I've installed cement using pip within my virtual environment context, however when I try to include it I get import errors.我已经在我的虚拟环境上下文中使用 pip 安装了水泥,但是当我尝试包含它时,我遇到了导入错误。

From the root of my project folder;从我的项目文件夹的根目录;

Project$ source dev/bin/activate
(dev) Project$ pip list
cement (2.10.2)
pip (9.0.1)
setuptools (36.6.0)
wheel (0.30.0)

Everything looks good.一切看起来都不错。

(dev) Project$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cement.core.foundation import CementApp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cement.py", line 1, in <module>
    from cement.core.foundation import CementApp
ImportError: No module named core.foundation
>>> quit()

So I cd into the virtual environment directory;于是我cd进入虚拟环境目录;

(dev) Project$ cd dev/
(dev) dev$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cement.core.foundation import CementApp
>>>

The import works fine form within the virtualenv directory.导入在 virtualenv 目录中工作正常。

Is this correct behaviour?这是正确的行为吗? Do I need to store my src files inside the virtualenv directory?我需要将我的 src 文件存储在 virtualenv 目录中吗? As I was under the impression my apps could live outside the virtualenv directory but access installed modules once in the virtualenv was active?因为我的印象是我的应用程序可以存在于 virtualenv 目录之外,但是一旦在 virtualenv 中访问已安装的模块处于活动状态?

Is this correct behaviour?这是正确的行为吗?

No, this is not correct behavior.不,这不是正确的行为。 After you activate the virtualenv, you should be able to run python from any directory, even outside your project, and from cement.core.foundation import CementApp should work.激活 virtualenv 后,您应该能够从任何目录运行python ,甚至在您的项目之外,并且from cement.core.foundation import CementApp应该可以工作。

Do I need to store my src files inside the virtualenv directory?我需要将我的 src 文件存储在 virtualenv 目录中吗?

No, you must absolutely not move your source files inside the virtualenv directory.不,您绝对不能将源文件移动到 virtualenv 目录中。

As I was under the impression my apps could live outside the virtualenv directory but access installed modules once in the virtualenv was active?因为我的印象是我的应用程序可以存在于 virtualenv 目录之外,但是一旦在 virtualenv 中访问已安装的模块处于活动状态?

That is correct, it should work.这是正确的,它应该工作。

The only thing I can think of that could be preventing it from working is that your virtualenv directory ( dev ) is somehow broken / corrupted.我能想到的唯一可能阻止它工作的是您的 virtualenv 目录( dev )以某种方式损坏/损坏。 I recommend to recreate it, which should be very easy.我建议重新创建它,这应该很容易。

This is correct behavior.这是正确的行为。 You install project related packages inside your virtual environment so that it does not mix up with the ones in the original python-pip that you have for your computer.你在你的虚拟环境中安装了与项目相关的包,这样它就不会与你计算机的原始 python-pip 中的包混淆。 Version mismatch is a serious problem at times, and to prevent that we use virtual environments.版本不匹配有时是一个严重的问题,为了防止我们使用虚拟环境。

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

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