Module gets imported under a syntactically false name space? - python

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?

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.

pip install --user (with emphasized --user) installed the package into your user directory: /home/Mohr/.local/lib/pythonX.Y/site-packages/.
To run your WSGI application you probably use a virtual environment in which the user-installed modules are not available. To use modules in the venv you have to install everything in the venv. So activate the venv in a terminal and install the module with the venv's pip:
pip install vsearch-1.0.tar.gz

Related

Package installed but doesn't import package

For some odd reason no matter which package I install when I go to import it doesn't know what package I'm talking about. I am very certain this is a Visual Studio Code error but if not I am also using Linux.
When I pip install the package pyttsx3 this is what I get in the Terminal:
Collecting pyttsx3
Downloading https://files.pythonhosted.org/packages/24/4e/580726c73272344d3e74b7aaffae55ff6b6450061fbecb8cc6e112531c02/pyttsx3-2.7.tar.gz
Building wheels for collected packages: pyttsx3
Running setup.py bdist_wheel for pyttsx3 ... done
Stored in directory: /home/secretlloyd/.cache/pip/wheels/a2/8a/fe/11112aca9c89142c3a404bc67ef3393a7ad530da26639a05d4
Successfully built pyttsx3
Installing collected packages: pyttsx3
Successfully installed pyttsx3-2.7
But when I run a example I get this error:
Traceback (most recent call last):
File "/home/secretlloyd/Visual Studio Code/Python/Finished/Text Colors/finished.py", line 1, in <module>
import pyttsx3
ModuleNotFoundError: No module named 'pyttsx3'
You can use an virtual environment to install your libs. If you do that, each project will have its own scoped libs without affect your global libs.
How to use the virtual environment?
Enter the root folder of your project and then run the following commands on the bash:
$ py -m venv .env
$ source .env/Scripts/activate
After that you'll notice your bash will have a prefix like that (.env). Then you should install your libs:
(.env) $ pip install pyttsx3
In order to deactivate the virtual environment just run the following command:
(.env) $ deactivate
Setup VS Code Intellisense for Virtual Environment
If you're using VSCode you can set the correct python interpreter after setting up a virtual environment. Just follow the steps:
Open VSCode in your project
Press F1
Type: > python: select interpreter
Click on Enter path or find an existing interpreter
Click on Find
The navigate to .env > Scripts > python
3 possible cases:
The same thing happened to me when I did not notice I was using two Pythons at the same time one 2.7 and another one 3.6. Make sure to know where is your package being installed to the Python modules folder you really want to store it or in another one you did not know existed.
Your PATH might not be configured correctly, check out either if you are using Windows or Linux if your PATH variables are configured correctly. You can reset your configuration if you wish. (link= How to reload .bashrc settings without logging out and back in again?)
For some packages/libraries of Python the way of importing the library is different from the name you import it on your .py file. For example: You can install OpenCV library by [pip install OpenCV] but when importing it in a file you have to write [import cv2].
I hope you find this information helpful for your problem.

ModuleNotFoundError after pip install . (custom module)

I am starting a custom package called nate-givens-toolkit.
I want to use a module from that package in a Jupyter notebook in a different folder.
Here is my structure
- nate-givens-toolkit/
- setup.py
- nate_givens_toolkit/
- cloud_io.py
- __init__.py
- project/
- project_nb.ipynb
I am going into nate_givens_toolkit (in the console) and running: pip3 install .
The output I get looks like everything is just fine.
Processing /home/ec2-user/nate-givens-toolkit
Building wheels for collected packages: nate-givens-tooklit
Building wheel for nate-givens-tooklit (setup.py) ... done
Created wheel for nate-givens-tooklit: filename=nate_givens_tooklit-0.1-py3-none-any.whl size=2009 sha256=7de8c9d2930d531603c973c7d8079b66f3d4326fb274e63087128fb7d25d9e1b
Stored in directory: /home/ec2-user/.cache/pip/wheels/17/96/2f/0073c92cfdadbb032d855f24df4725bf190d39cd1c5bb1d233
Successfully built nate-givens-tooklit
Installing collected packages: nate-givens-tooklit
Attempting uninstall: nate-givens-tooklit
Found existing installation: nate-givens-tooklit 0.1
Uninstalling nate-givens-tooklit-0.1:
Successfully uninstalled nate-givens-tooklit-0.1
Successfully installed nate-givens-tooklit-0.1
But if I go into project_nb.ipynb and write:
from nate_givens_toolkit import cloud_io as cloud
I get: ModuleNotFoundError: No module named 'nate_givens_toolkit'
The same thing happens if I run python from the console. As long as I'm in the nate-givens-toolkit directory I can import and run it fine. But if I navigate up a level and try the import I get ModuleNotFoundError.
I don't understand why pip install seems to work, but I still get this ModuleNotFoundError. I thought the whole point of the pip install would be that I could then import from files in other directory without having to worry about relative paths and such. (I've done this exact same thing in a totally different context for work and it was fine.)
I've tried the solutions for similar issues like these:
after pip successful installed: ModuleNotFoundError
ModuleNotFoundError: No module named 'requests' after pip install
So far, haven't found anything that works.
Everything I'm doing is inside the same conda venv.
Help?
I found the answer to my question here: Import py file in another directory in Jupyter notebook. (Note: it wasn't the accepted answer on that post, it was the second answer after the accepted answer.)
The trick is that I needed to do use the -e flag when using pip install.
So instead of
pip install .
I had to run
pip install -e .
Once I ran that, it worked in Python from the console and also in Jupyter.

How to import python module after cloning and modifying the module's github repo

I'm trying to make an open source contribution to a python module that is hosted on Github (pypika).
I cloned the repo from github and ran pip in editable install mode such that any future imports would point to my version of the code.
But when I try running a test file within the repo, I get an error when trying to import the module. What am I doing wrong? How can I make it so that the import will use the modified module that I'm working on?
$ cd Dev
$ git clone https://github.com/kayak/pypika.git
$ pip install -e /Users/me/Dev/pypika
Obtaining file:///Users/me/Dev/pypika
Installing collected packages: PyPika
Running setup.py develop for PyPika
Successfully installed PyPika
$ python3 ./pypika/pypika/tests/test_functions.py
Traceback (most recent call last):
File "./pypika/pypika/tests/test_functions.py", line 3, in <module>
from pypika import (
ImportError: No module named 'pypika'
PyPika maintainer here. In order to contribute it's best to make a fork on Github and put your contributions in a separate branch which you can then pull request.
In order to run the tests you simply need to execute python -m unittest in the project folder of PyPika. (Or use the Python test runner functionality of your favourite IDE.)
pip install -e /Users/me/Dev/pypika will work if executed in the environment of the project in which you want to use PyPika.

I cannot import a python submodule after successful installation of the module

I am trying to install the python submodule ANCA in my mac, for which I have run the following:
pip install git+https://github.com/acadev/anca.git
getting the following:
.
.
.
Installing collected packages: anca
Running setup.py install for anca ... done
Successfully installed anca-0.1.5
I have miniconda installed in my computer, and I have checked that the module and its files and dependencies are correctly located in /usr/local/miniconda2/lib/python2.7/site-packages/anca
When I import anca in my notebook it does not give me any error, but if I try to import a submodule it gives me the following error:
from anca import IterativeMeansAlign
ImportError: cannot import name IterativeMeansAlign
It is the same with other submodules, but all of them are apparently well installed with all their files.
I have also tried to install the package using Git:
git clone https://github.com/acadev/anca.git
sudo python setup.py install
with the same result.
Note: 'locate' is not able to locate the module anca unlike the rest of the python modules although I can see all the files in the right location. It is the first time this happens to me.
Thanks a lot for your help,
any suggestion will be very appreciated.

Unable to use azure SDK in Python

I did this:
sudo pip install azure azure-storage azure-servicebus azure-mgmt azure-servicemanagement-legacy
from azure import *
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure
from azure.storage import BlobService
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure.storage
Python package installed thru cmd sudo pip install exists at the paths /usr/lib/python2.7, /usr/local/python2.7, etc and their sub-folder dist-packages.
You can code import sys and print sys.path in the Python Interpreter to show the completed path list for current python environment.
Iif you installed successfully some packages like azure & azure-storage, you can find these files relate to the package in the python library paths.
However, you got the error in Import Error: No module named <package-name> when you run the code import <package-name> or from <package-name> import <class or object name>. There are two scenes that would be cause the issue normally.
Package not installed successfully.
The library path included package that not exists in Python system environment path sys.path in python or PYTHONHOME in environment variables.
So I think you can try to solve the issue thru three ways below.
Dynamically add the package path into sys.path thru the method sys.path.append('<package path>') in python code.
Check the environment variable PYTHONHOME whether or not set up. If set up PYTHONHOME, python will add package path based on PYTHONHOME into sys.path.
If your python environment encounter some unknown fault that is unrecoverable, you can try to reinstall Python thru commands sudo apt-get remove python python-pip, sudo apt-get update, sudo apt-get install python python-pip on Ubuntu. It's a simple way.
BlobService belongs to azure.storage.blob rather than the azure.storage
it should rather be
from azure.storage.blob import BlobService
Link - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/
If it still doesn't work for you, you might would like to use virtualEnv and do the pip install again while in virtualenv
http://docs.python-guide.org/en/latest/dev/virtualenvs/
I had very similar issue. There was a lot of confusion between python2 and python3 package versions as there was no virtual env used and I also had to ungrade pip to 18.
But anyway, this is is how I resolved the part in question.
Locate where the package was installed:
pip show azure
The output will show the location of the package in the Location section:
Name: azure
Version: 4.0.0
Summary: Microsoft Azure Client Libraries for Python
Home-page: https://github.com/Azure/azure-sdk-for-python
Author: Microsoft Corporation
Author-email: azpysdkhelp#microsoft.com
License: MIT License
Location: /usr/local/lib/python3.6/dist-packages
Requires: azure-servicefabric, azure-cosmosdb-table, azure-datalake-store, azure-loganalytics, azure-eventgrid, azure-servicemanagement-legacy, azure-servicebus, azure-graphrbac, azure-storage-blob, azure-mgmt, azure-storage-file, azure-batch, azure-applicationinsights, azure-keyvault, azure-storage-queue
Required-by:
If you do:
python -c "import sys;print(sys.path)"
You will see a list of pip package locations:
['/app', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
At the begining of my python file I added:
sys.path.insert( 0, '/usr/local/lib/python3.6/dist-packages' )
This will make sure this package location will be checked in the first place.
UPDATE
Thinking about it in the morning, things opened from a new perspective for me. I saw, that I had #!/usr/bin/python at the beginning of my python file, which says to use the wrong interpreter and look for pip packages in the wrong place.
azure metapackage is deprecated
and azure-storage is not being maintained anymore
Please use azure-storage-blob >= 12.0
pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient
PS: I write SDKs for azure

Categories