Search code examples
pythonmodulesetuptoolspythonanywhere

Module gets imported under a syntactically false name space?


I am following along with the O'Riley Head First Python (2nd Edition) Course. At one point you will create a webapp and deploy it to pythonanywhere (chapter5).

The webapp uses two functions, imported from a module, created earlier.

The module is called vsearch.py. I also created a readme.txt and a setup.py and used setuptools to create a source distribution file using :

python3 setup.py sdist

The code of the setup.py read as follows:

from setuptools import setup

setup(
    name = "vsearch",
    version = "1.0",
    description = "The Head First Python Seach Tools",
    author = "HF Python 2e",
    author_email = "hfpy2e@gmail.com",
    url = "headfirstlabs.com",
    py_modules = ["vsearch"],
)

The source distribution file gets created without errors and creates a file called vsearch-1.0.tar.gz

The file then gets uploaded to pythonanywhere and installed via console using: python3 -m pip install vsearch-1.0.tar.gz --user

Console outputs:

15:36 ~/mysite $ python3 -m pip install vsearch-1.0.tar.gz --user
Looking in links: /usr/share/pip-wheels
Processing ./vsearch-1.0.tar.gz
Building wheels for collected packages: vsearch
  Running setup.py bdist_wheel for vsearch ... done
  Stored in directory: /home/Mohr/.cache/pip/wheels/85/fd/4e/5302d6f3b92e4057d341443ed5ef0402eb04994663282c12f7
Successfully built vsearch
Installing collected packages: vsearch
  Found existing installation: vsearch 1.0
    Uninstalling vsearch-1.0:
      Successfully uninstalled vsearch-1.0
Successfully installed vsearch-1.0

Now when I try to run my webapp I get the following error:

2020-03-24 16:18:14,592: Error running WSGI application
2020-03-24 16:18:14,592: ModuleNotFoundError: No module named 'vsearch'
2020-03-24 16:18:14,593:   File "/var/www/mohr_eu_pythonanywhere_com_wsgi.py", line 16, in <module>
2020-03-24 16:18:14,593:     from vsearch4web import app as application  # noqa
2020-03-24 16:18:14,593: 
2020-03-24 16:18:14,593:   File "/home/Mohr/mysite/vsearch4web.py", line 3, in <module>
2020-03-24 16:18:14,593:     from vsearch import search4letters

Judging from this error I assume that "vsearch" can not be found because it was installed as "vsearch-1.0". However when I try to change this line to:

from vsearch-1.0 import search4letters

I rightfully get a synthax error since I can not adress modules this way. So what can I do about this? When creating the module in the beginning I added a version number to the setup.py file because according to the lecture it is good practice. Setuptools then automatically creates the source distribution file with the "-1.0" at the end. Also when importing it using the command shown above i automatically gets importet as "vsearch-1.0" which in turn I am unable to reference in my python code because of bad synthax.

Am I doing something wrong? Is there a way to import this under another namespace? Is there a way to reference "vsearch-1.0" in my python code without getting a synthax error?


Solution

  • There are different python3 versions installed on PythonAnywhere. When you install something using python3 -m pip or pip3 you use default python3 that is probably not matching python version setting of your web app. Use python3.7 and pip3.7 or python3.6 and pip3.6 etc. for --user installations to be sure.