2

VirtualEnv + Apt + Python3 Issue

On 14.04 I've got an interesting problem trying to import a module. I have python3 and python3-apt installed and I can verify this with the following:

    chalbersma@j2test:~$ python3
    Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import apt_pkg
    >>> 

Which is great! This is how I developed the application I'm trying to develop. So now I go ahead and I want to create a virtual env so that I can add some modules without messing up the system as a whole.

    mkdir test
    virtualenv -p python3 ./test
    <stuff>
    chalbersma@j2test:~$ source ./test/bin/activate
    (test)chalbersma@j2test:~$ 

So now I'm in my test market. I expect my imports to show up in both so when I try to use apt

    (test)chalbersma@j2test:~$ python3
    Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import apt_pkg
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named 'apt_pkg'
    >>> 

I also tried to pip install it with pip install apt and pip install apt_pkg but neither of those work. This sort of functionality seems to work with other modules. What am I missing?

  • Looks like there is python-apt on PyPI that you can use pip to install but that's for Python 2. For Python 3, your solution for using the system-site-packages is probably the only way to get the apt_pkg module. – edwinksl Jun 23 '16 at 22:08
1

Found the answer here. You need to use the --system-site-packages option when creating your virtualenv if you want this to work. So in my example the correct way to do it would be

virtualenv --system-site-packages -p python3 ./test

Thanks for helping guys!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.