Linux python importerror no module named

Python error «ImportError: No module named»

I have already checked sys.path and there I have the directory /site-packages . Also, I have the file __init__.py.bin in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin in the examples directory. I do not know why Python cannot find the file when it is in sys.path . Any ideas? Can it be a permissions problem? Do I need some execution permission?

Check that you have read permission to that file from python. See: stackoverflow.com/a/20999950/1657225

The problem in my case was that there was the permission to newly installed modules were not 755 . That was because umask on the machine was 0027 due to which the others did not have read permission causing module to not be read. Adding read permission fixed my problem. It’s worth checking the permission of the target directory post-installation.

38 Answers 38

Based on your comments to orip’s post, I guess this is what happened:

  1. You edited __init__.py on windows.
  2. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
  3. You used WinSCP to copy the file to your unix box.
  4. WinSCP thought: «This has something that’s not basic text; I’ll put a .bin extension to indicate binary data.»
  5. The missing __init__.py (now called __init__.py.bin ) means python doesn’t understand toolkit as a package.
  6. You create __init__.py in the appropriate directory and everything works. ?

Also, python -c ‘import sys; print sys.path’ helps — sometimes the user has placed the files in a path not scanned.

For me the issue was I was using python driver.py when I should have been using python3 driver.py since I installed with pip3 .

As @EricWiener pointed out, using python 2 may cause this issue aswell. He may be using #!/usr/bin/env python to detect the python environment, and have python2 instead of python3 mapped, which causes that error in some packages

I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:

(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)

  1. Change directory (cd) to the directory above the directory where your files are. In this case, you’re trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.
  2. When you are in the toolkit directory, enter this line of code on your command line: export PYTHONPATH=. This sets your PYTHONPATH to «.», which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn’t just look in your current directory, but in all the directories that are in your current directory).
  3. After you’ve set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.
Читайте также:  Все дистрибутивы linux ubuntu

Be careful if you have third party or custom library references in an already existing PYTHONPATH environment variable. If you do the instead run: set PYTHONPATH=%PYTHONPATH%;.; to append . to the PYTHONPATH and then echo %PYTHONPATH% which should display path\to\custom\library;.; . Then run your application from the application directory with python applicationlaunchfile.py .

(local directory)/site-packages/toolkit 

To make import walk through your directories every directory must have a __init__.py file.

Good point! Remark: Since Python 3.3, any directory on sys.path with a name that matches the package name will be recognised.

__init__.py is not needed since 3.3 (although it has some other effects and is often desirable). See peps.python.org/pep-0420.

You are reading this answer says that your __init__.py is in the right place, you have installed all the dependencies and you are still getting the ImportError .

I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn’t have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:

export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory) 

There’s another way to do this using sys.path as:

import sys sys.path.insert(0,'') OR sys.path.append('') 

You can use insert/append based on the order in which you want your project to be searched.

This is what I did as a workaround. But I still don’t understand why this should be necessary. I have, in my Pipfile, a relative path module that cannot be detected (e.g. I get the ImportError) without said workaround.

This is great. A general way to apply it if you’re sure you’re in the right path: sys.path.append(os.getcwd())

On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:

(Mind the .: at the beginning, so that it can search on the current directory, too.)

It may also be in other locations, depending on the version:

 .:/usr/lib/python .:/usr/lib/python2.6 .:/usr/lib/python2.7 and etc. 

It may also be .:/usr/lib/python , .:/usr/lib/python2.6 , .:/usr/lib/python2.7 and etc. depending on the version

For me, the module is in in /usr/local/lib/python3.4/dist-packages but when I type python3 in the terminal (ubuntu), and try to import it, it doesn’t allow me, saying that it doesn’t exist. «ImportError: no module x exists»

Читайте также:  Show all logged in users linux

Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root

thank you, this is definitely needed inheriting legacy projects with all modules in a flat hierarchy outside of a /src directory

For me, it was something really stupid. I installed the library using pip3 install but was running my program as python program.py as opposed to python3 program.py .

Thank you! Think this is a common mistake if you’re running on a system that has Python2 installed by default (OS X)

I solved my own problem, and I will write a summary of the things that were wrong and the solution:

The file needs to be called exactly __init__.py . If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.

Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python , and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.

Источник

How to fix «ImportError: No module named . » error in Python?

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 

7 Answers 7

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 .

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.

@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.

What worked for me was to add to $PYTHONPATH the parent directory of the project, in your case will be: /home/bodacydo/work . see this answer

Do you have a file called __init__.py in the foo directory? If not then python won’t recognise foo as a python package.

See the section on packages in the python tutorial for more information.

Thanks and yes, I had __init__.py . The problem this time was with $PYTHONPATH . Ignacio’s solution worked.

The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file

Читайте также:  Linux list installed packages version

A better fix than setting PYTHONPATH is to use python -m module.path

This will correctly set sys.path[0] and is a more reliable way to execute modules.

I have a quick writeup about this problem, as other answerers have mentioned the reason for this is python path/to/file.py puts path/to on the beginning of the PYTHONPATH ( sys.path ).

Here is a step-by-step solution:

    Add a script called run.py in /home/bodacydo/work/project and edit it like this:

import programs.my_python_program programs.my_python_program.main() 

Explanation: Since python appends to PYTHONPATH the path of the script from which it runs, running run.py will append /home/bodacydo/work/project . And voilà, import foo.tasks will be found.

Example solution for adding the library to your PYTHONPATH.

    Add the following line into your ~/.bashrc or just run it directly:

export PYTHONPATH="$PYTHONPATH:$HOME/.python" 
ln -s /home/user/work/project/foo ~/.python/ 

In my mind I have to consider that the foo folder is a stand-alone library. I might want to consider moving it to the Lib\site-packages folder within a python installation. I might want to consider adding a foo.pth file there.

I know it’s a library since the ./programs/my_python_program.py contains the following line:

from foo.tasks import my_function

So it doesn’t matter that ./programs is a sibling folder to ./foo . It’s the fact that my_python_program.py is run as a script like this:

Источник

ImportError: No module named — Python

Your modification of sys.path assumes the current working directory is always in main/ . This is not the case. Instead, just add the parent directory to sys.path :

import sys import os.path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import gen_py.lib 

Don’t forget to include a file __init__.py in gen_py and lib — otherwise, they won’t be recognized as Python modules.

@Ceren This code should work in every IDE. Your IDE (or more precisely, the Python plugin for it) may not actually execute the Python code, in which case you probably need to configure the search path somewhere. Feel free to ask a new question about that.

@phihag Thanks! I had asked a question about an hour ago, feel free to answer it =) stackoverflow.com/questions/17038416/…

Is it possible to put this somewhere not in the my main application? I used this solution to solve my similar error but now I have a file that looks messy with imports all over the place. I thought there was a way to do this in the init file but I can’t find good examples.

What does sys.path.append(os.path.join(os.path.dirname(__file__), ‘..’)) resolve to? Why isn’t it sys.path.append(os.path.join(‘..’, os.path.dirname(__file__))) ?

For the Python module import to work, you must have «src» in your path, not «gen_py/lib».

When processing an import like import gen_py.lib , it looks for a module gen_py , then looks for a submodule lib .

As the module gen_py won’t be in «../gen_py/lib» (it’ll be in «..»), the path you added will do nothing to help the import process.

Depending on where you’re running it from, try adding the relative path to the «src» folder. Perhaps it’s sys.path.append(‘..’) . You might also have success running the script while inside the src folder directly, via relative paths like python main/MyServer.py

Источник

Оцените статью
Adblock
detector