I have a config.yaml file that I want to import in my app.py.
This is my folder structure:
/root
__init__.py
app.py
config.yaml
App.py:
import logging
import config
from flask import Flask
app = Flask(__name__)
logger = logging.getLogger(__name__)
app.run(port=5001)
I have no problem importing other Python modules (.py) but when I try to import my YAML file, i get:
ImportError: No module named config
Does anyone know why this does not work? Do I need any dependencies to be able to import a YAML file like this?
Why do you expect to be able to import a YAML file? It isn't Python, and it isn't an extension module, and these are the only things that Python will import.
YAML (Yet Another Markup Language) is a data format, so you have to read the data with a suitable library: this answer might give you some clues.
You cannot import YAML files directly into Python as you suggest in your example - only *.py files. You can import the yaml Python module and parse your file, as shown in these SO answers:
How can I parse a YAML file in Python
Parsing a YAML file in Python, and accessing the data?
Related
I have made a small programming language and seperated the Lexer, Parser, and Interpreter into different files. Now I would like those files to be in a sub directory Source. I have Shell.py that uses them. In short, this is the structure.
Language -{
Source -{
Main.py
Lexer.py
Parser.py
Interpréter.py
Shell.py
In shell .py, I want to import main.py, which in turn imports the Lexer, parser, and interpreter.
So:
#Shell.py
import Source.Main
Main.run(some code)
#Main.py
from Lexer import Lexer
.... Parser
.... Interpreter
When I run Main.py everything works, but when I run Shell.py it comes up with this:
File Source/Main.py, line 1 in <module>
from Lexer import Lexer
ImportError: No module named ‘Lexer’
EDIT:
There is an _init_.py in the Source directory.
I am not trying to import multiple files from the sub directory, just one that procedes to import others.
Importing a file does not automatically import all the packages you imported in the file. Basically, when you imported Main.py, you didn't import the packages that Main.py imports. You need to manually import all of the packages in that folder.
EDIT: Based on a discussion in the comments, you need to change import omega to from Source import omega assuming Source is what you called the folder where your other files is stored.
I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common
I am trying to resolve an issue with python packages PySpark. I developed a python package which has the following structure.
sample_package/
|-config/
|-sample.ini
|-main.py
|-__init__.py
Inside my main.py, I have a code snippet that reads the config file from the config/ directory as follows
import ConfigParser, os
def sample_func():
config = ConfigParser.ConfigParser()
configfile = os.path.join(os.path.dirname(__file__), 'config', 'sample.ini')
config.read(configfile)
return config.sections()
I created a zip file of the above package as sample_package.zip and included the zip as a pyspark dependency
addPyFile(path/to/zip/file)
In my pyspark job when i import the sample_package the import works fine and i'm able to call the sample_func inside main, but however my python package is unable to read the sample.ini file. When executed inside a plain python program, it works fine but not inside a pyspark job. Is there any path manipulation being done in a pyspark environment when accessing static files? How can I get my python package to properly read the config file?
I figured out the answer by myself. It is more of a python packaging issue rather than pyspark environment issue. Looks like I had to use pkgutil module to reference my static resources which modifies my function as below
import ConfigParser, os, pkgutil, StringIO
def sample_func():
config = ConfigParser.ConfigParser()
configfile = pkgutil.get_data('sample_package', 'config/sample.ini')
cf_buf = StringIO.StringIO(configfile)
config.readfp(cf_buf)
return config.sections()
More simple version:
from configparser import ConfigParser
import pkgutil
def sample_func():
config = ConfigParser()
# os.path.join is not needed.
config_data = pkgutil.get_data(__name__, 'config/sample.ini').decode()
config.read_string(config_data)
return config.sections()
How do I explicitly tell Google App Engine (python) to import json from the python standard libraries?
Due to a poorly named file (that I can NOT change or rename at this time) I am having trouble importing json.
There is a json.py in the same directory as the file that I am working on.
When I try:
import json
it imports the file in the same directory.
Is there a way I can do something along the lines of:
from ../ import json
to import the Native JSON library?
EDIT:
I have now even tried renaming the offending file and replacing all uses of this file. But I'm still not able to import the json standard lib through GAE.
Attached is the error log:
File "/Users/admin/Blah/dataaccess.py", line 13, in <module>
from classes import ZenDesk
File "/Users/admin/Blah/classes/ZenDesk.py", line 10, in <module>
import json
File "/Users/admin/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 892, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named classes.json
Please Help google app engine should be looking for the standard library and not in the subdirectory classes
If you can't rename the module for some reason, you have to mess around with sys.path to put your standard lib in front of the current module. Make sure to fix it again after you do your import, though...
import sys
sys.path = [r'C:\Python27\Lib'] + sys.path
import json
sys.path = sys.path[1:]
Alternately, I'm pretty sure the imp module has functionality for this.
Let me be clear, though: it would be much better (as others have said) to just rename the module in the project directory (e.g. to my_json.py). You're not going to have to change that many dependencies.
Some Edits:
If you cannot find an actual path, absolute or otherwise, to the standard lib, you have two further options besides renaming the thing. You can move the file you're working on to a different level in your package (so that it can't see the local json.py). For instance, like this:
/package
__init__.py
your_file.py # "import json" should now hit the std lib.
# To import the local copy, use "from app import json"
app/
__init__.py
json.py
other_thing.py # contains "import json" to get the local copy
If you don't want to move your file, then you can fake it by adding an extra file that does nothing but import the real json module:
/package
__init__.py
json_importer.py # contains nothing but "import json"
app/
__init__.py
json.py
your_file.py # "from package.json_importer import json"
other_thing.py # contains "import json" to get the local copy
Try this perhaps?
import imp
json = imp.load_source('module.name', '/path/to/built-in/json/file.py')
Try this, although I'm not sure if it will work on GAE since its a PaaS platform:
import os
import sys
from distutils.sysconfig import get_python_lib
sys.path.insert(0,os.path.dirname(get_python_lib()))
import json
sys.path = sys.path[1:]
For my gae python project, I'd like to import an external library named 'vobject'. What's the correct way to import it in my .py files?
The project's readme says that in order to install it, you need to run
python setup.py install
Additionally, vobject requires the 'dateutil' package.
Since this is going to run on GAE, I thought I should copy both libs over into my project instead of running the install script to make use of it in my code.
But I'm getting a bunch of import errors and I'm not sure what the correct convention is for external gae/python libs.
utc = dateutil.tz.tzutc()
## error produced:
File "myGaeProject/external/vobject/icalendar.py", line 47, in <module>
NameError: name 'dateutil' is not defined
Because of the way I've structured my project, I changed icalendar.py's import structure from:
import dateutil.rrule
import dateutil.tz
to:
import external.dateutil.rrule
import external.dateutil.tz
I also tried:
from external.dateutil import *
What's the correct import mechanism for a project structured like so:
-myGaeProject
--external
----__init__.py
----dateutil
------__init__.py
------tz.py
------rrule.py
------[more dateutil files]
----vobject
------__init__.py
------base.py
------icalendar.py
--handlers
------__init__.py
------mainHandler.py
Don't modify the library. If you want to put all your libraries in external, you need to add external to your python path before you attempt to import libraries from there:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'external'))
import some_external_library
You can't do from external import dateutil if external is missing an __init__.py file.
The good way is to use zipimport, you can check the project jaikuengine they are a lot of good things about that.
http://code.google.com/p/jaikuengine/source/browse/trunk/build.py
In Jaiku, all external libs are stocked in the directory vendor but if you see the app.yaml, all files in vendor are skipped.
Jaiku uses a script to build a zip of each libs in vendor and put it to the root of the project before the deployment or when the dev_server is launched.
With that, you don't need to fix the path of your libs.
EDIT an example to load all zipped archives
Largely inspired from jaikuengine:
def load_zipped(path='.'):
for x in os.listdir(path):
if x.endswith('.zip'):
if not any([y.endswith(x) for y in sys.path]):
sys.path.append(os.path.abspath('%s/%s') % (path, x))