Asked  5 Months ago    Answers:  5   Viewed   3k times

My python somehow can't find any modules in the same directory. What am I doing wrong? (python2.7)

So I have one directory '2014_07_13_test', with two files in it:

  1. test.py
  2. hello.py

where hello.py:

# !/usr/local/bin/python
# -*- coding: utf-8 -*-

def hello1():
    print 'HelloWorld!'

and test.py:

# !/usr/local/bin/python
# -*- coding: utf-8 -*-

from hello import hello1

hello1()

Still python gives me

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 4, in <module>
ImportError: No module named hello

What's wrong?

 Answers

48

Your code is fine, I suspect your problem is how you are launching it.

You need to launch python from your '2014_07_13_test' directory.

Open up a command prompt and 'cd' into your '2014_07_13_test' directory.

For instance:

$ cd /path/to/2014_07_13_test
$ python test.py

If you cannot 'cd' into the directory like this you can add it to sys.path

In test.py:

import sys, os
sys.path.append('/path/to/2014_07_13_test')

Or set/edit the PYTHONPATH

And all should be well...

...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'

There is a better shebang you should use.

Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.

Friday, July 9, 2021
 
Andres
answered 5 Months ago
39

The working directory is not set to the directory of the script, but to the current directory where you started the script.

Use __file__ to determine the file location and use that as a starting point to make filename an absolute path:

import os

here = os.path.dirname(os.path.abspath(__file__))

filename = os.path.join(here, '20140210204804.kml')
Wednesday, August 4, 2021
 
Guesser
answered 4 Months ago
78

@ImportanceOfBeingErnest lead me in the right direction. I post my solution here s.t. others may find the answer. The problem was a corrupted disk sector - an unlikely event of chance.

The problem was indeed in the matplotlib-package itself. Retrospectively, pointers to the issue were that errors in pre-distributed packages should not exist. If they do, external circumstances must have corrupted and the problem is not with the Python-installation itself.

I uninstalled matplotlib through Anaconda Prompt with conda remove matplotlib and re-installed with conda install matplotlib. This gave me this error:

(C:Anaconda3) C:UsersJohn>conda install matplotlib
[...]
ERROR conda.core.link:_execute_actions(337): An error occurred while installing package 'defaults::matplotlib-2.0.2-np112py36_0'.
OSError(22, 'Invalid argument') Attempting to roll back. 

OSError(22, 'Invalid argument')

Before @Ernest's comment, I thought it maybe had to do with non-ASCII in PATH or similar.

Instead I tried to reinstall Anaconda3 completely, restarted and found that part of the Anaconda3-folder weren't removed (the one containing the matplotlib).

Deleting it manually gave a Windows error 0x80070570. Following this post on ServerFault (the comment to OP) I ran a check and afterwards a repair from Windows Explorer GUI: Right-click on the drive in This PC --> Properties --> Tab Tools --> Check (repair appears if any errors are found).

After some restarts, reinstalling Anaconda3 from scratch and restarting again, I was able to run my project again!

Friday, August 6, 2021
 
Pravi
answered 4 Months ago
46

I'm not sure you can remove Python 2 altogether, but you can install Python 3 and evoke it specifically instead of Python 2.

First, make sure Python 3 is Installed: Log into your Synology via the web administration and in the 'Package Center' make sure 'Python 3' is installed. (You will find it under 'Developer Tools' within the 'Package Center')

Next, specifically evoke Python 3 by simply calling python3

You could also try using the locate command on your Synology to find the exact location of Python 2. To do this enter the following on your Synology:

ipkg update
ipkg install mlocate

And here is a tutorial on how to use the command: http://www.thegeekstuff.com/2012/03/locate-command-examples/

Good luck!

Also, If you don't have luck on Stack Overflow I'd run over to the Synology Forums for help as well: https://forum.synology.com

Friday, August 13, 2021
 
Sidarta
answered 4 Months ago
53

You need to add __init__.py in your core folder. You getting this error because python does not recognise your folder as python package

After that do

from .posts_run import get_all_posts
#    ^ here do relative import
# or
from core.posts_run import get_all_posts
# because your package named 'core' and importing looks in root folder
Friday, October 29, 2021
 
Dennis
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share