Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
Jamie Avatar asked Feb 15 '26 00:02

Jamie


1 Answers

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.

like image 59
janos Avatar answered Feb 17 '26 15:02

janos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!