Import Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Python Importerror Cannot Import Name
Us Learn more about Stack Overflow the company Business Learn more about hiring import error -9912 image capture developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the python importerror no module named windows Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to fix “ImportError: No module
Django Importerror: No Module Named
named …” error in Python? up vote 47 down vote favorite 6 What is the correct way to fix this ImportError error? I have the following directory structure: /home/bodacydo /home/bodacydo/work /home/bodacydo/work/project /home/bodacydo/work/project/programs /home/bodacydo/work/project/foo And I am in the directory /home/bodacydo/work/project Now if I type python ./programs/my_python_program.py I instantly get ImportError: No module named foo.tasks The ./programs/my_python_program.py contains the following line: from foo.tasks import my_function I can't
Importerror: No Module Named Pycharm
understand why python won't find ./foo/tasks.py - it's there. If I do it from the Python shell, then it works: python >>> from foo.tasks import my_function It only doesn't work if I call it via python ./programs/my_python_program.py script. python share|improve this question edited Jul 9 '15 at 20:36 neuronet 1,26511344 asked Feb 24 '10 at 12:31 bodacydo 14.5k38126228 add a comment| 5 Answers 5 active oldest votes up vote 52 down vote accepted Python does not add the current directory to sys.path, but rather the directory that the script is in. Add /home/bodacydo/work/project to either sys.path or $PYTHONPATH. share|improve this answer answered Feb 24 '10 at 12:47 Ignacio Vazquez-Abrams 446k64797954 So whats the point then of having init.py in the root folder? It seems to serve no purpose if you edit either sys.path or the pythonpath. –user1980175 Jan 4 '14 at 23:13 6 @Editor: __init__.py only indicates that the directory should be treated as a package, when its parent is either in sys.path or is itself a package. –Ignacio Vazquez-Abrams Jan 4 '14 at 23:15 In addition check the permissions on the folder. That was the problem in my case. &nda
and then inside that for an object named baz (which may be a regular python object, or another module) A module is defined as: either a python importerror no module named site Python file ie a file on disk that ends in .py and contains valid
Importerror: No Module Named Mac
Python (syntax errors, for example, will stop you from being able to import a file) or a folder which contains $pythonpath Python files. for a folder to become a module, it must contain a special file called __init__.py When a module is actually a folder, the things you can import from it are: any http://stackoverflow.com/questions/2325923/how-to-fix-importerror-no-module-named-error-in-python other modules that are inside the folder (ie, more .py files and folders) any objects defined or imported inside the __init__.py of the folder Finally, where does Python look for modules? It looks in each directory specified in the special "sys.path" variable. Typically (but not always), sys.path contains some default folders, including the current working directory, and the standard "packages" directory for that system, usually called site-packages, which https://help.pythonanywhere.com/pages/DebuggingImportError/ is where pip installs stuff to. So from foo.bar import baz could work in a few different ways: . `-- foo/ |-- __init__.py `-- bar.py <-- contains a variable called "baz" Or . `-- foo/ |-- __init__.py `-- bar/ |-- __init__.py `-- baz.py Or: . `-- foo/ |-- __init__.py `-- bar/ `-- __init__.py <-- contains a variable called "baz" What this means is that you need to get a few things right for an import to work: The dot-notation has to work: from foo.bar import baz means foo has to be a module folder, and bar can either be a folder or a file, as long as it somehow contains a thing called baz. Spelling mistakes, including capitalization, matter The top-level "foo" must be inside a folder that's on your sys.path. If you have multiple modules called "foo" on your sys.path, that will probably lead to confusion. Python will just pick the first one. Debugging sys.path issues in web apps can you run the wsgi file itself? $ python3.4 -i /var/www/www_my_domain_com_wsgi.py Or, if you're using python 2: $ python2.7 -i /var/www/www_my_domain_com_wsgi.py Or, if you're using a virtualenv, activate it first: $ workon my-virtualenv (my-virtualenv)$ python -i /var/www/www_my_domain_com_wsgi.py If this show
started with python -I had a hard time figuring out how to import third-party modules & packages into my program. Here's a http://knowpapa.com/import/ gist of what I finally learnt: Know the Python Path Knowing about the PYTHONPATH is key to installing and importing third-party packages. When an import command is passed, https://github.com/pallets/flask/issues/1902 python looks for the module/package in a list of places. The default path(s) where python would search for modules can be found out by: import sys print no module sys.path For my computer it gives me the the following paths: [", ‘C:\\Python27\\Lib\\idlelib', ‘C:\\Windows\\system32\\python27.zip', ‘C:\\Python27\\DLLs', ‘C:\\Python27\\lib', ‘C:\\Python27\\lib\\plat-win', ‘C:\\Python27\\lib\\lib-tk', ‘C:\\Python27', ‘C:\\Python27\\lib\\site-packages'] Starters would do good to install or add modules in one of these paths. If you get this wrong, you will get an error like: ImportError: No module named "foo" . By convention, all third-party packages no module named go into Pythonfolder/Lib/site-packages. Importing Module or Importing packages ? When you add a third-party module to your python library, remember that simply dumping a folder with bunch of .py files will not make it a package. When you use the import command, python does not look inside folders, even if the folders are located in the PYTHONPATH. However, if a folder in the PythonPath has a file called __init__.py, Python jumps inside it and treats the complete folder as a package. Changing the Pythonpath If you want to add packages in any other directory (other than the python paths listed above) - you will have to first export the python path in your code with: PYTHONPATH=$PYTHONPATH:C/Your new path/yournewmodulefolder Virtual Environment Builder Suppose you build several applications in your Python environment where one of your application requires version 1 of PackageX, while another application of yours requires version 2 of PackageX. If you try placing both these version in the same folder (say site-package), you are i
Sign in Pricing Blog Support Search GitHub This repository Watch 1,568 Star 23,108 Fork 7,342 pallets/flask Code Issues 106 Pull requests 38 Projects 0 Wiki Pulse Graphs New issue Import error with some examples #1902 Closed rnelsonchem opened this Issue Jun 9, 2016 · 15 comments Projects None yet Labels bug cli docs Milestone 1.0 Assignees No one assigned 10 participants rnelsonchem commented Jun 9, 2016 I'm trying to run the "flaskr" and "minitwit" examples, but I'm running into an import error issue. I'm trying to follow the instructions from the README file. When running flask initdb, I get the following error: Traceback (most recent call last): File "/home/nelson/apps/miniconda/bin/flask", line 6, in