Barka
Barka

Reputation: 8922

installing Python package from github into virtual environment works but import fails

  1. I created a python project named PythonApplication
  2. I created a virtual environment env (Python 3.6 (64-bit))
  3. I run the following in the virtual environment directory

pip install git+git://github.com/BillMills/python-package-example.git

I get: enter image description here

Looking at the directory structure at

..\PythonApplication\PythonApplication\env\Lib\site-packages\myPackage

I see enter image description here

and

enter image description here

  1. I then add the following code inside PythonApplication.py

import myPackage

foo = 6
bar = 7

When I run this I get the following error:

ModuleNotFoundError: No module named 'somePython'

What did I miss?

Based on Saurav's comment below I re-did the steps. However, after creating the environment I did activate it at the command prompt and ran the pip command inside the activated environment instead of the command prompt. The results did not change.

Someone else commented that github.com/BillMills/python-package-example.git is written in Python 2 and will not work in Python 3. If that is correct, what needs to be changed?

enter image description here

Upvotes: 0

Views: 1547

Answers (1)

avigil
avigil

Reputation: 2246

The package you are using from https://github.com/BillMills/python-package-example is using an import style not supported in Python 3. You can see in python-package-example/__init__.py that

import somePython

is used to import a submodule, but python 3 will assume somePython exists as a top level module. See PEP-328 for a better explanation.

A Python 3 compatible example package can be found at https://github.com/kennethreitz/samplemod. Notice that in sample/__init__.py submodules are imported using a relative import (indicated by the leading '.'):

from .core import hmm 

If you wanted to modify python-package-example to be compatible you would need to change its __init__.py to use an absolute import:

import myPackage.somePython as somePython

or a relative import:

from . import somePython

Upvotes: 2

Related Questions