简体   繁体   中英

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).

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.

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;

(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.

Is this correct behaviour? Do I need to store my src files inside the virtualenv directory? As I was under the impression my apps could live outside the virtualenv directory but access installed modules once in the virtualenv was active?

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.

Do I need to store my src files inside the virtualenv directory?

No, you must absolutely not move your source files inside the virtualenv directory.

As I was under the impression my apps could live outside the virtualenv directory but access installed modules once in the virtualenv was active?

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. 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. Version mismatch is a serious problem at times, and to prevent that we use virtual environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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