Python Import Site Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might importerror no module named site python have Meta Discuss the workings and policies of this site About
Importerror No Module Named Site Python Linux
Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
Importerror No Module Named Site Python Windows
ads 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
Importerror: No Module Named Site Ubuntu
programmers, 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 apache importerror: no module named site 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 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 p
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and gdb importerror: no module named site policies of this site About Us Learn more about Stack Overflow the importerror no module named site uwsgi company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users importerror: no module named site django Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/2325923/how-to-fix-importerror-no-module-named-error-in-python a minute: Sign up Python import-site error with multiple versions up vote 0 down vote favorite I'm using a shared machine. I'm trying to install Python 2.6 for a project that is using Pysco (unsupported on 2.7), and there is an existing install of 2.7 on the machine that I cannot remove without breaking other people's projects. I'm using 32-bit binaries of http://stackoverflow.com/questions/6471388/python-import-site-error-with-multiple-versions Python on Windows 7. When I try to start Python 2.6, I get this error: 'import-site' failed; use -v for traceback It looks like Python 2.6 is trying to import modules from Python 2.7, which is causing the failure (see output below). Does anybody know what might be causing this, and how it could be fixed? Thanks! Running with the -v option gives the following output # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py import os # precompiled from C:\Python27\Lib\os.pyc import errno # builtin import nt # builtin # C:\Python27\Lib\ntpath.pyc matches C:\Python27\Lib\ntpath.py import ntpath # precompiled from C:\Python27\Lib\ntpath.pyc # C:\Python27\Lib\stat.pyc matches C:\Python27\Lib\stat.py import stat # precompiled from C:\Python27\Lib\stat.pyc # C:\Python27\Lib\genericpath.pyc matches C:\Python27\Lib\genericpath.py import genericpath # precompiled from C:\Python27\Lib\genericpath.pyc # C:\Python27\Lib\warnings.pyc matches C:\Python27\Lib\warnings.py import warnings # precompiled from C:\Python27\Lib\warnings.pyc # C:\Python27\Lib\linecache.pyc matches C:\Python27\Lib\linecache.py import linecache # precompiled from C:\Python27\Lib\linecache.pyc # C:\Python27\Lib\types.pyc matches C:\Python27\Lib\types.py import types # precompiled from C:\Python27\Lib\types.pyc # C:\Python27\Lib\UserDict.pyc matches C:\Python27\Lib\UserDict.py import UserDict # precompiled from C:\Python27\Lib\UserDict.pyc # C:\Python27\Lib\_abcoll.pyc matches C:\Python27\Lib\_abcoll.py import _a
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 http://knowpapa.com/import/ finally learnt: Know the Python Path Knowing about the PYTHONPATH is key http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html to installing and importing third-party packages. When an import command is passed, 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 sys.path For my computer it gives me the the no module 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 go into Pythonfolder/Lib/site-packages. Importing Module or Importing packages ? When you add a no module named 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 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
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 General Linux Notes Python Conferences Nick Coghlan's Python Notes Docs » Python Concepts » Traps for the Unwary in Python's Import System Edit on Bitbucket Traps for the Unwary in Python's Import System¶ Python's 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 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 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 previous trap: if a subdirectory encountered on sys.path as part of a package import contains an __init__.py file, then the Python interpreter will create a single directory package containing only modules from that directory,