Asked  2 Years ago    Answers:  5   Viewed   5.8k 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

1

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.

Wednesday, October 26, 2022
 
vytis
 
3

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')
Thursday, August 25, 2022
 
macdada
 
1

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

Wednesday, October 12, 2022
 
2

Finally got it sorted.

Apparently for Windows users pip.ini config file is not created, however can be added manually!

just create new %APPDATA%pippip.ini and content of custom repository:

[install]
find-links = https://<login>:<password>@your.repo.com/custom/url

Reference: https://pip.pypa.io/en/stable/user_guide/#config-file

Thursday, August 11, 2022
 
minion
 
5

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 I'd run over to the Synology Forums for help as well: https://forum.synology.com

Thursday, November 24, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages