OK guys I can't find a solution anywhere for my problem, and I hope the solution is a simple one. Previously I had a flat file system for my gae project with no folders. I've been refactoring some code and I tried to put some in a folder. I'm a bit new and I've never done something like this before, but nothing on the internet suggests that I shouldn't easily be able to move my files into a folder. I added the __init__.py file to the folder and I import the folder name from my main program. However when I attempt to access a particular function in one of the files, it chokes and says AttributeError: 'module' object has no attribute 'site1_ripper'
here is my file structure:
main.py
SiteCrawlers\
__init__.py
site1_ripper.py
here are important parts of the files:
main.py
import SiteCrawlers
class Updater(webapp.RequestHandler):
def get(self):
SiteCrawlers.site1_ripper.siteCrawler()
site1_ripper.py
def siteCrawler()
#stuff here
I think the problem is that you need to explicitly import site1_ripper unless it's specified in __init__.py. Make your main import be:
import SiteCrawlers.site1_ripper
In your main file try:
from SiteCrawlers.site1_ripper import siteCrawler
class Updater(webapp.RequestHandler):
def get(self):
siteCrawler()
Related
I'm trying to import the User class from a file called gui.py into another called snake.py. I want to import the class so that I can use ,a method within the class and an instance of the class called current_user. But when I do:
from gui import User
It imports everything from gui.py. Does anyone know where I've gone wrong and what I can do to fix this?
I'm new to working with multiple files in Python and this is quite confusing to me.
The files for this are available at:
https://github.com/VladRadoi08/Snake-LoginUI
Thanks!
Anything in the file you import will be run. To prevent this, try putting all the code you don't want to run within this conditional:
if __name__ == "__main__":
That will ensure it only runs if you actually run the python file instead of importing it.
I tried writing some commenly used function in a seperate file and import the same into mainApp file, but not able to use import.
I did find many questions regarding the this same question but, the solution was to keep the files in the same folder
I tried without .py as well, but the same error:
Can you please help me how can i fix this issue ?
No '.py'. Just import seperate
Try using this in mainApp.py:
from seperate import *
a()
where seperate.py looks like this:
def a():
print('hi')
Well, sorry, those two files need to be in the same folder. This is not a solution to your problem.
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Read more about Absolute vs Relative Imports in Python
In your case it should be:
from .seperate import a
Also check this question:
Importing from a relative path in Python
add your project directory into your path variable so that python know from where you want to import file
I have 10 different python projects stored in one folder (F:\Python_Code...). I want to call user define functions from 10 different projects into the last project (Say Project11) and by running Project11, all my 10 projects should run one by one.
I have tried multiple ways like os.path() and from project1 import function, etc. but no one work. I read about the change in PYTHONPATH, but I am still not able to do that. I am using PyCharm. Can anyone help me to solve the problem?
soni smit!
Your solution wasn't that far away.
First you have to import the whole file with:
from . import filename
or just
import filename
if the file is in the same directory as your main file.
then you can call a function from that file with:
filename.functionname(arg1, arg2, ...)
I hope, it works for you!
~ostue
It's not a good idea to reference an upper-level directory for importing your packages.
If you're sure of what you're doing, you can change the working directory using os.chdir(path_to_dir_that_can_access_all_your_modules).
If you need the flexibility to import your libs in a dynamic way, try using importlib.import_module('module_name').
ex.:
import os, importlib
def import_module(base_path, module_path):
try:
backup_wd = os.getcwd() # backup original working directory
os.chdir(base_path) # change directory
return importlib.import_module(module_path) # import and return your module
except:
# Handle problems
...
finally:
os.chdir(backup) # go back to original directory in any case
project10_module = import_module('F:\Python_Code', 'project10.utils.yourmodule')
module_instance = project10_module(args)
Im having trouble importing a class on a python module.
Here are my directory structure:
_wikiSpider
+scrapy.cfg
_wikiSpider
+__init__.py
+items.py
+items.pyc
+settings.py
+settings.pyc
+pipelines.py
_spiders
+__init__.py
+__init__.pyc
+articleSpider.py
+articleSpider.pyc
+items.py
Code breaks at this line:
from wikiSpider.items import Article
Im not sure why, since class Article is defined at items.py (deepest folder)
Can someone give me an explanation?
Like others, I didn't have a circular reference problem. I'd like to generalize the solution here just a bit more though.
Any file name conflict can cause this. You could have multiple sub files with the same name (as above).
Or it could be the file you're working in.
Eg: trello.py as a pet project.
from trello import TrelloApi
Import reference will import itself before importing the pip installed package. Attempts to import trello and reference objects directly will fail with "NameError: name '' is not defined"
You have an items.py in both your root and _spiders folder. To reference a file in a subfolder you need the folder name and the file.
from _spiders.items import Article
assuming the file that imports this code is in your root directory. Python uses a you are here, to current file location, for it's directory hierarchy.
Best solution :
Rename class name with temporary name
Put the same temporary name in import statement in __init__.py
Now that works, put your old name again
from main wikiSpider directive try:
from _wikiSpider._spiders.items import Article
orelse from terminal open your _spiders directive and try:
from items import Article
Here we want to open the items.py file where we created Article class, so when you give some wrong directive or file , it cant find the items.py file that you created, so it shows 'Cannot import error'
What worked for me is removing the __pycache__ folder. I was moving class files around and changing the directory hierarchy up and the cache must have had old names/locations. Deleting it and running my program again created a new cache and the error was no longer present.
I am having trouble using my classes that I've defined in a module. I've looked at this stackoverlfow post, and the answer seems to be "you don't need imports." This is definitely not the behavior I'm experiencing. I'm using Python 3.3. Here is my directory structure:
root/
__init__.py
mlp/
__init__.py
mlp.py
layers/
__init__.py
hidden_layer.py
dropout_layer.py
My problem is this: the class defined in dropout_layer.py extends the class in hidden_layer.py, but when I try to import hidden_layer, I sometimes get an error depending on the directory I execute my code from. For instance, from layers.hidden_layer import HiddenLayer then I run my code if I execute it from root/mlp. This import does not work, however, if I execute my code from root. This is strange behavior to me. How can I get this working correctly?
My only non-empty __init__.py file is in root/mlp/layers/:
# root/mlp/layers/__init__.py
__all__ = ['hidden_layer', 'dropout_layer']
In Python 3 you can prepend a . for an import relative to the location of the current module:
from .hidden_layer import HiddenLayer