Python Import Module Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have importerror python cannot import name Meta Discuss the workings and policies of this site About Us
Import Error Python No Module Named
Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads python no module named error with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, importerror: no module named pycharm just like you, helping each other. Join them; it only takes a minute: Sign up How to fix “ImportError: No module 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
$pythonpath
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 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,28011344 asked Feb 24 '10 at 12:31 bodacydo 14.5k39126229 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 447k64800956 So whats the point then of having init.py in the root folder? It seems to serve no purpose if you edit eithe
The stale bytecode file trap The submodules are added to the package namespace trap More exotic traps Else Clauses on Loop Statements Various Ideas for Python and CPython Using the Python Kerberos Module python importerror no module named site General Linux Notes Python Conferences Nick Coghlan's Python Notes Docs » Python Concepts » __init__.py file Traps for the Unwary in Python's Import System Edit on Bitbucket Traps for the Unwary in Python's Import System¶ Python's
Import Error Python Windows
import system is powerful, but also quite complicated. Until the release of Python 3.3, there was no comprehensive explanation of the expected import semantics, and even following the release of 3.3, the details of how http://stackoverflow.com/questions/2325923/how-to-fix-importerror-no-module-named-error-in-python sys.path is initialised are still somewhat challenging to figure out. Even though 3.3 cleaned up a lot of things, it still has to deal with various backwards compatibility issues that can cause strange behaviour, and may need to be understood in order to figure out how some third party frameworks operate. Furthermore, even without invoking any of the more exotic features of the import system, there are quite http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html a few common missteps that come up regularly on mailing lists and Q&A sites like Stack Overflow. This essay only officially covers Python versions back to Python 2.6. Much of it applies to earlier versions as well, but I won't be qualifying any of the explanations with version details before 2.6. As with all my essays on this site, suggestions for improvement or requests for clarification can be posted on BitBucket. The missing __init__.py trap¶ This particular trap applies to 2.x releases, as well as 3.x releases up to and including 3.2. Prior to Python 3.3, filesystem directories, and directories within zipfiles, had to contain an __init__.py in order to be recognised as Python package directories. Even if there is no initialisation code to run when the package is imported, an empty __init__.py file is still needed for the interpreter to find any modules or subpackages in that directory. This has changed in Python 3.3: now any directory on sys.path with a name that matches the package name being looked for will be recognised as contributing modules and subpackages to that package. The __init__.py trap¶ This is an all new trap added in Python 3.3 as a consequence of fixing the previ
started with python -I had a hard time figuring out how to import third-party modules & packages into my program. Here's a gist of what I finally learnt: Know http://knowpapa.com/import/ the Python Path Knowing about the PYTHONPATH is key to installing and importing third-party packages. When an import command is passed, python looks for the module/package in a list of places. http://kronosapiens.github.io/blog/2014/07/28/understanding-package-imports-in-python.html The default path(s) where python would search for modules can be found out by: import sys print sys.path For my computer it gives me the the following paths: [", ‘C:\\Python27\\Lib\\idlelib', ‘C:\\Windows\\system32\\python27.zip', no module ‘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 go into Pythonfolder/Lib/site-packages. Importing Module or Importing packages ? When you add a third-party module to your python library, remember that no module named 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 in for some trouble. To avoid any such interference, you can have separate Virtual environment for each of your applications using Virtual Python Environment builder. Possibly RelatedOpen Source Android App to Calculate Sunrise Sunset TimeAndroid: Adding Glow Effect to UI elementsOpenCV: Interlacing
what seems to be an incidental change. Tests will run in one directory but not another, but then inadvertently start working, only to stop a few days or minutes later. I’ve tried to be methodical in investigating what changes lead to what behavior, but it’s been difficult. To hopefully put this to rest, I’m going to investigate and methodically record all the behavior I can isolate regarding package imports, to hopefully make some sense of what’s going on. PYTHONPATH First, let’s start with a project I’m calling ‘backend’. Here’s the file structure: backend/ backend/ __init__.py analyzer.py tests/ __init__.py test_analyzer.py And my PYTHONPATH: ƒ: echo $PYTHONPATH /Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/backend/: So, my PYTHONPATH is pointing to the directory containing the backend package, but not to the backend package itself (which contains __init__.py). Let’s open up a terminal and play around: >>> import backend >>> backend <module 'backend' from 'backend/__init__.pyc'> Very cool. Now let’s cd into the backend package and see if anything changes: >>> import backend >>> backend <module 'backend' from '/Users/kronosapiens/Dropbox/Documents/Development/code/jobs/paragon/backend/backend/__init__.pyc'> That’s curious. Let’s see what happens when we remove the path to the project from our PYTHONPATH. ƒ: