when i use http://github.com/joshthecoder/tweepy-examples ,
i find :
import tweepy
in the appengine\oauth_example\handlers.py
but i can't find a tweepy file or tweepy's 'py' file, except a tweepy.zip file,
i don't think this is right,cauz i never import a zip file,
i find this in app.py:
import sys
sys.path.insert(0, 'tweepy.zip')
why ?
how to import a zip file..
thanks
updated
a.py :
import sys
sys.path.insert(0, 'b.zip')
import b
print b
b.zip:
b file
|-----__init__.py
|-----c.py
c.py:
cc='ccccc'
the error is :
> "D:\Python25\pythonw.exe" "D:\zjm_code\a.py"
Traceback (most recent call last):
File "D:\zjm_code\a.py", line 9, in <module>
import b
ImportError: No module named b
updated2
it is ok now ,
the error's reason is : i rename b.rar to b.zip
The name of the zip file is irrelevent when searching for modules - this allows you to include version numbers in the file name, such as my_b_package.1.2.3.zip.
To import from a zip file, you need to replicate the full package structure within it. In this case, you need a package b, with the __init__.py and c.py modules.
I.e:
b.zip
|
| -- b <dir>
| -- __init__.py
| -- c.py
You don't import zip files, you add them to sys.path so that you can import modules within them. sys.path is a list, and as such the normal list methods/operations (e.g. .append()) all work on it.
Related
my file directory is as follows
mein_paket
--src
----__init__.py
----script.py
--tests
----test.py
In my tests/test.py file, I have the following code:
from mein_paket.src.script import Cat
c = Cat('aicri')
c.meow()
However I get the error ModuleNotFoundError: No module named 'mein_paket'
I have tried different importing methods such as
from mein_paket.src.script import Cat
from mein_paket.src import Cat
from src.script import Cat
from src import Cat
But none seem to work, I'm also trying to avoid the use of sys.append.
I have a following directory structure:
source
source_1.py
__init__.py
source1.py has class Source defined
source1.py
class Source(object):
pass
I am able to import using this
>>> from source.source1 import Source
>>> Source
<class 'source.source1.Source'>
However when trying to import using the below method it fails.
>>> from source import *
>>> source1.Source
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'source1' is not defined
Please let me know how can we use the 2nd import ?
For importing from a package (unlike importing from a module) you need to specify what * means. To do that, in __init__.py add a line like this:
__all__ = ["source1"]
See the Python documentation for Importing * From a Package.
In the following structure:
src:
model:
- boardmodel.py
- tests.py
exceptions:
- exceptions.py
- visual.py
I'm running visual.py from the terminal. In my boardmodel.py, I have the following import:
from exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
This line causes an error when running visual.py:
Traceback (most recent call last):
File "visual.py", line 3, in <module>
from model.boardModel import Board
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/boardModel.py", line 5, in <module>
from exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
ModuleNotFoundError: No module named 'exceptions'
What is the correct way to solve this problem? Should I change the import statement inside boardmodel.py to from model.exceptions.exceptions import ZeroError....? That doesn't feel like a sustainable solution, because what if I want to use boardmodel.py in another context? Let me her your thoughts.
EDIT:
I changed the structure to:
src:
model:
- __init__.py
- boardmodel.py
- tests.py
exceptions:
- __init__.py
- exceptions.py
- visual.py
But still I get the same error.
Place an empty file called __init__.py in each of the subdirectories to make them packages.
See What is __init__.py for? for more details
Then, within boardmodel.py you need to either use relative imports
from .exceptions.exceptions import ...
or absolute ones (from the root of PYTHONPATH)
from model.exceptions.exceptions import ...
When you execute a script from the command line, the directory containing the script is automatically added to your PYTHONPATH, and becomes the root for import statements
You are not at the root of your package so you need relative import.
Add a dot before exceptions:
from .exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
One dot mean "from the current directory"…
I have a problem importing a module:
It is under this directory ./dao and the code that calls it is here ./core. Schematically represented as:
rnaspace/
__init__.py
core/
__init__.py
logger.py
dao/
__init__.py
storage_configuration_reader.py
This is the error message:
Traceback (most recent call last): File "logger.py", line 21, in <module>
from rnaspace.dao.storage_configuration_reader import storage_configuration_reader ImportError: No module named rnaspace.dao.storage_configuration_reader
This file it is there /rnaspace/dao/storage_configuration_reader.py and in the same folder the __init__.py file as follows:
""" Package dao
Gathers files that access to the plateform data
"""
If I understood well this question, it should work. I think that the problem is that one is not the subdirectory of the other (or that the path is not exaclly that one), there is a way to go around it? Or need I to apply the solution to this question?
EDIT
The __init__.py file of the rnaspace folder:
import rnaspace.dao.storage_configuration_reader as scr
def update_conf(conf_path, predictors_conf_dir):
scr.update_conf(conf_path, predictors_conf_dir)
from rnaspace.dao.storage_configuration_reader import storage_configuration_reader
That is wrong because there is no "storage_configuration_reader" directory in "dao" directory
This is how it should be:
from rnaspace.dao import storage_configuration_reader
EDIT:
or this way:
import rnaspace.dao.storage_configuration_reader
I finally found the solution in another question, it is using the module imp.
I just needed to add the name of the module, and the absolute path where it was:
imp.load_source("storage_configuration_reader","./rnaspace/dao/storage_configuration_reader.py")
I'm trying to use importlib.import_module in Python 2.7.2 and run into the strange error.
Consider the following dir structure:
a
|
+ - __init__.py
- b
|
+ - __init__.py
- c.py
a/b/__init__.py has the following code:
import importlib
mod = importlib.import_module("c")
(In real code "c"has a name.)
Trying to import a.b, yields the following error:
>>> import a.b
Traceback (most recent call last):
File "", line 1, in
File "a/b/__init__.py", line 3, in
mod = importlib.import_module("c")
File "/opt/Python-2.7.2/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named c
What am I missing?
Thanks!
For relative imports you have to:
a) use relative name
b) provide anchor explicitly
importlib.import_module('.c', 'a.b')
Of course, you could also just do absolute import instead:
importlib.import_module('a.b.c')
I think it's better to use importlib.import_module('.c', __name__) since you don't need to know about a and b.
I'm also wondering that, if you have to use importlib.import_module('a.b.c'), why not just use import a.b.c?
And don't forget to create a __init__.py with each folder/subfolder (even if they are empty)