I have some troubles understanding how python import works.
I just forked this repository and I am going to play with it but i am having troubles understanding how python import works with file in directories.
The directory structure is the following:
fitbit/
__init__.py
gather_keys_cli.py
api.py
exceptions.py
utils.py
As a preliminary step I need to run gather_keys_cli.py. It depends on api.py, which in turn depends on exceptions.py.
If I try to run it from the python interpreter everything works as expected. If I try to run it from the command line (As suggested in the documentation) I have obtain the following exception.
$./fitbit/gather_keys_cli.py KEY SECRET
Traceback (most recent call last):
File "./fitbit/gather_keys_cli.py", line 34, in <module>
from api import FitbitOauthClient
File "/Users/mariosangiorgio/FitBitHacks/python-fitbit/fitbit/api.py", line 9, in <module>
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
ImportError: No module named fitbit.exceptions
My understanding is that when I invoke the command line the base path is set to the folder containing it. At this point, the scripts correctly imports the oauth client because it uses this instruction from api import FitbitOauthClient, which does not contain any reference to the fitbit directory.
On the opposite, api.py uses this instruction to import exceptions from fitbit.exceptions import (LIST_OF_CLASSES). This instruction contains the reference to the fitbit directory and hence I got the error.
I am not familiar with python so I'd like to understand what is the pythonic way to solve this issue.
I have the feeling that there is something clearly wrong going on and I found a workaround by moving gather_keys_cli.py and changing the api import to import fitbit.api. It works but I am not sure it is the right way to solve the issue I'm experiencing.
Related
I am trying to run my python scripts on my IIS website using CGI and I am having trouble with importing. When run on its own, the python scripts finds the mysql.connector module installed in my os perfectly fine, but when I try to run it on the website, it gives a Bad Gateway (502.2) error with the stacktrace stating ModuleNotFoundError: No module named 'mysql.connector'. I'm assuming CGI cannot find the module in my OS, how can I let it find the module? Do I have to specify my modules folder somewhere in the IIS like a PATH variable?
Here is the complete stacktrace of the bad gateway page:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Traceback (most recent call last): File "C:\Users\pedro\OneDrive\Documents\adet\ind.py", line 2, in import python_mysql File "C:\Users\pedro\OneDrive\Documents\adet\python_mysql.py", line 1, in import mysql.connector ModuleNotFoundError: No module named 'mysql.connector' ".
As a workaround, I simply copied and pasted all of my modules in my modules folder to my website's folder. I guessed that since my personal imports were working that it should also work if I added the other modules to the same folder and lo, it did. I hope there's a cleaner way to solve this, but for now I'll choose this as a solution.
My colleagues shared some programs and modules with me. I put them in ...../anaconda3/lib/python3.7/site-packages/pdbParser. Below show the content.
I set this directory in $PATH. However, when I run my program at another directory (let's say user directory), I received below error message.
File "run.py", line 20, in <module>
from pdbParser.pdbParser import pdbParser
File "/<my system dir>/anaconda3/lib/python3.7/site-packages/pdbParser/__init__.py", line 14, in <module>
from __pkginfo__ import __version__, __author__
ModuleNotFoundError: No module named '__pkginfo__'
Actually, my program (run.py) can call the module (pdbParser.py) in "//anaconda3/lib/python3.7/site-packages/pdbParser/" but the module (pkginfo.py) called by pdbParser.py cannot be found. I do not understand why it happened. I read related questions (1 and 2) in this community, but I could not get the issue fixed. Is there anything wrong on my end? Any further help and suggestion would be highly appreciated.
Can you fix the error syntax in the __init__.py file like this?
append .
from
from __pkginfo__ import __version__, __author__
to
from .__pkginfo__ import __version__, __author__
I am trying to make my code Python code run, however, I am running into import errors (shown below). I have looked up some possible solutions and no luck after trying them out.
My directory level is fairly simple.
Python
pkgA
__init__.py
verify.py
console.py
I used to be able to run console.py and was working fine, however, I've been trying to clean up the imports and make the python package installable.
When I run console.py it throws the following error.
Traceback (most recent call last):
File "/Repositories/Python/pkgA/console.py", line 3, in <module>
from ..pkgA import verify
ValueError: attempted relative import beyond top-level package
My console.py has this import from ..supv import verify
I've tried manipulating the paths to try to make it work, but it hasn't been successful.
I have been trying to import python module "Pulp" to the Amazon AWS Lambda but getting an error. Pulp is an optimization module which can be installed using pip ("pip install pulp") but as in AWS Lambda I'm not sure how to install it so I zipped everything along with Lambda Function from my local machine and uploaded it to AWS Lambda.
The error which I received:-
"Attempted relative import in non-package: ValueError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 5, in lambda_handler
import pulp
File "/var/task/pulp.py", line 101, in
from .constants import *
ValueError: Attempted relative import in non-package"
Here is the link for .zip file https://drive.google.com/open?id=0B7SjHToKYgr3cXlHenpoOFljMDg
Thanks in advance.
Follow the instructions on http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
On how to deploy to AWS lambda using virtualenv.
The error is a standard Python error saying that it will only import things from the standard locations. Since that includes the current directory you should be good but your code includes
from . import pulp
when you have pulp.py in your directory. If pulp.py was all that was needed you would be fine. But pulp.py wants to import .constants. That is a relative reference which would be fine since it is supposed to be in a module. In your case it is not fine. If you want to keep going on this path you will have to go through and remove these relative imports.
Also your .zip file includes .pyc files. Let these be generated on the target machine. Just send your .py's that they came from.
I had a file called baseFunctions.py which I changed to elementFunctions.py. Importing elementFunctions with parsePeaksMzML.py gave me weird print statements, especially because I did not have any print statements in elementFunctions.py. Stepping through the code with Eclipses debugger showed instead of doing
import elementFunctions
as it said in the parsePeaksMzML.py, it actually did
import baseFunctions
I thought there was some weird mix-up with the renaming, so I copied all the code from elementFunctions.py, deleted the file, made a new file and pasted in all the code in the new file. Still I got the same weird errors. Looking in the folder I still had the baseFunctions.pyc, so I deleted that. Rerunning the code gave me:
Traceback (most recent call last):
File "/homes/ndeklein/workspace/MS/Trunk/PyMS_dev/pyMS/test/test_parsePeaksMzML.py", line 25, in <module>
import parsePeaksMzML
File "/homes/ndeklein/workspace/MS/Trunk/PyMS_dev/pyMS/test/parsePeaksMzML.py", line 12, in <module>
import elementFunctions
ImportError: No module named baseFunctions
Somehow import elementFunctions points to import baseFunctions (I have no clue why) and deleting and remaking elementFunctions.py has no effect. I can't find anything like this searching on google or stack overflow, so I'm kind of stumped here.
I suspect it to be a problem with eclipse, but I'm not sure.
edit:
What makes it even stranger, I get the error when I import parsePeaksMzML.py from
test_parsePeaksMzML.py, but not when I run parsePeaksMzML.py directly.
edit2:
Running from the commandline gives the same error:
-bash-3.2$ python test_parsePeaksMzML.py
Traceback (most recent call last):
File "test_parsePeaksMzML.py", line 26, in <module>
import parsePeaksMzML
File "/homes/ndeklein/workspace/MS/Trunk/PyMS_dev/pyMS/test/parsePeaksMzML.py", line 12, in <module>
ImportError: No module named baseFunctions
-bash-3.2$
If you changed the name of a file, probably you have some old .pyc lingering in your workspace. I'm assuming you're using the PyDev plug-in in Eclipse; I haven't used it in a while, but you should be able to do a Project -> Clean. If that doesn't help, try manually clearing all of the .pyc files in your workspace.
The symptoms you describe are very strange, so I'm not 100% sure what the problem is. Regardless, trying cleaning your workspace and let us know how things progress from there.
Do try a blanket delete of all .pyc files and see if that clears it up. If not, here are a few possibilities:
Your $PYTHONPATH points to different source than you expect. (Though this would likely not be the entire explanation.)
Code within elementFunctions/__init__.py is trying to import baseFunctions.
Code somewhere is using an exec statement to do imports, or using the built-in __import__ function to dynamically import baseFunctions (which might make the source of the directive difficult to hunt down).