Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 5414  / 2 Years ago, tue, july 26, 2022, 11:29:27

I'm trying to run a simple python program on ubuntu server 12.04. However I get an import error for a module I've written. The module folder has an init.py folder in however python does not seem to find it giving the following error



user1@cl:~/git/p1/test$ ls
modules prog1.py
user1@cl:~/git/p1/test$ python prog1.py
Traceback (most recent call last):
File "prog1.py", line 8, in <module>
from modules import find
ImportError: No module named modules


From reading associated question it would appear I need to set a python path. Could anyone tell me exactly what files I need to modify, I've seen suggestions including .bashrc .bash_profile and .profile. And also what directory I need to point the path too. I'm not totally sure if this should be my home directory or the exact folder my program is in, or the folder the module is in.



Sorry if this is a very basic question I'm new to ubuntu


More From » server

 Answers
6

From your question, I am not sure what is the precise situation, but to set a (any) path from a python program or script:



import sys
sys.path.insert(0, "/path/to/your/module(s)")


However, looking at the first two lines of your question, both the program and the modules folder are in the same directory, which makes setting the path unnecessary. It looks like there is someting wrong with your __init__.py file; you mention an "init.py" folder, but is there a (empty) file named __init__.py in it?



The modules structure should be:



modules (folder)
|___ __init__.py
|___ file1.py
|___ file2.py


etc.



to import:



from modules import file1, file2 (file1, 2 without extension)

[#25554] Wednesday, July 27, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alliulet

Total Points: 46
Total Questions: 109
Total Answers: 97

Location: Svalbard and Jan Mayen
Member since Sat, Oct 10, 2020
4 Years ago
;