Linux python import error

Can’t import my own modules in Python

I’m having a hard time understanding how module importing works in Python (I’ve never done it in any other language before either). Let’s say I have:

myapp/__init__.py myapp/myapp/myapp.py myapp/myapp/SomeObject.py myapp/tests/TestCase.py 
myapp.py =================== from myapp import SomeObject # stuff . TestCase.py =================== from myapp import SomeObject # some tests on SomeObject 
ImportError: No module named myapp 

While the answers to this question explain how to solve the problem, the reason is explained here: stackoverflow.com/questions/24435697/…

14 Answers 14

In your particular case it looks like you’re trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do

since it is in the same folder. For TestCase.py, do

from ..myapp import SomeObject 

However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py , you would have to mess with your path. This can be done within Python:

import sys sys.path.append("..") from myapp import SomeObject 

though that is generally not recommended.

In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you’re serious about the package, you could even add it to the Python Package Index, PyPI.

Источник

How to resolve import errors in python?

I have a specific problem which might require a general solution. I am currently learning apache thrift. I used this guide.I followed all the steps and i am getting a import error as Cannot import module UserManager. So the question being
How does python import lookup take place. Which directory is checked first. How does it move upwards?
How does sys.path.append(») work?

I found out the answer for this here. I followed the same steps. But i am still facing the same issue. Any ideas why? Anything more i should put up that could help debug you guys. ? Help is appreciated.

5 Answers 5

On windows, Python looks up modules from the Lib folder in the default python path, for example from «C:\Python34\Lib\». You can add your Python libaries in a custom folder («my-lib» or sth.) in there, but you need a file in order to tell Python that you can import from there. This file is called __init__.py , and is totally empty. That data structure should look like this:

from my-lib import mymodule 

And then just using the name of you function. You can now use sys.path.append to append the path you pass into the function to the folders Python looks for the modules (Please note that thats not permanent). If the path of your modules should be static, you should consider putting these in the Lib folder. If that path is relative to your file you could look for the path of the file you execute from, and then append the sys.path relative to your file, but i reccomend using relative imports. If you consider doing that, i recommend reading the docs, you can do that here: https://docs.python.org/3/reference/import.html#submodules

Читайте также:  Server monitoring tool linux

my folders name is gen-py and i have the init.py inside it. and my python file lies outside that folder. Here is how my python file look. import sys sys.path.append(‘/gen-py’) even this does not work

Python is looking is handling /gen-py as a full path. You should define the real full path, this means like «C:/Users/Saras/Desktop/python/gen-py/». You can get the path of a file like this: import os; os.path.realpath(__ file __) file of course without the blank spaces (just for editing).

If I got you right, you’re using Python 3.3 from Blender but try to include the 3.2 standard library. This is bound to give you a flurry of issues, you should not do that. Find another way. It’s likely that Blender offers a way to use the 3.3 standard library (and that’s 99% compatible with 3.2). Pure-Python third party library can, of course, be included by fiddling with sys.path.

The specific issue you’re seeing now is likely caused by the version difference. As people have pointed out in the comments, Python 3.3 doesn’t find the _tkinter extension module. Although it is present (as it works from Python 3.2), it is most likely in a .so file with an ABI tag that is incompatible with Blender’s Python 3.3, hence it won’t even look at it (much like a module.txt is not considered for import module). This is a good thing. Extension modules are highly version-specific, slight ABI mismatches (such as between 3.2 and 3.3, or two 3.3 compiled with different options) can cause pretty much any kind of error, from crashes to memory leaks to silent data corruption or even something completely different.

You can verify whether this is the case via import _tkinter; print(_tkinter.file) in the 3.2 shell. Alternatively, _tkinter may live in a different directory entirely. Adding that directory won’t actually fix the real issue outlined above.

Источник

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.

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 .

Читайте также:  Linux полностью удалить пакет

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»

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.

Источник

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