0

I am trying to follow @Eric Leschinski exmaple 7 solution on how to import other python files. I belive he is using a Linux base operating system since his path are in the format of el@apollo:/home.... I am using Windows and am not quite sure how to replicate his solution in regards to the paths. I don't quite understand the @ and $ symbols and if I need to change them?

D:\Echo\herp\__init__.py:

D:\Echo\herp$ touch__init__.py
D:\Echo\herp$ ls
__init__.py

D:\Echo\herp\derp\__init__.py:

D:\Echo\herp\derp$ touch__init__.py
D:\Echo\herp\derp$ ls
__init__.py

D:\Echo\herp\derp\yolo.py:

def skycake():
  print("SkyCake evolves to stay just beyond the cognitive reach of " + "the bulk of men. SKYCAKE!!")

D:\Echo\main.py

from herp.derp.yolo import skycake
skycake()
Traceback (most recent call last):
  File "D:/Echo/main.py", line 1, in <module>
    from herp.derp.yolo import skycake
  File "D:\Echo\herp\__init__.py", line 1
    D:\Echo\herp$ touch__init__.py
                                 ^
SyntaxError: unexpected character after line continuation character
Binx
  • 382
  • 7
  • 22
  • 2
    it seems you have `D:\Echo\herp$ touch__init__.py` in file but this is NOT `python code` but `console prompt` with `console command`. You have totally wrong code in this file. – furas Aug 05 '21 at 01:02
  • 1
    code `D:\Echo\herp$` means prompt displayed in console - so it shows that you have to run it in console - not put in file. And `touch __init__.py` is console command `touch` which creates empty file `__init__.py` . And `ls` is console command to display files in directory - and this command should display `__init__.py`. And you shouldn't put it in `__init__.py`. You need empty `__init__.py` and all these lines are used in Linux only to create empty file (and check if it exists). – furas Aug 05 '21 at 01:09
  • 1
    The `__init__.py` files should be blank. The code that you show is something you use *at the command prompt* in order to *create blank files* (and verify that they exist). – Karl Knechtel Aug 05 '21 at 01:32
  • 1
    You should read the answer more carefully. Notice how it says "Make an empty file named __init__.py under herp"? – Karl Knechtel Aug 05 '21 at 01:33
  • @KarlKnechtel, you are correct. I did read that incorrectly. It seemed that that code was placed in the `__init__.py` which is why I was confused. – Binx Aug 05 '21 at 14:37

1 Answers1

1

You understood it in wrong way.

el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

el@apollo:/home... is Linux console prompt which shows that it has to be executed in console.

touche __init__.py is Linux command touch which creates empty file __init__.py.

ls is Linux command to display files in folder and it displays __init__.py (which you see in last line)

All this is only to show how to create empty files __init__.py in console in Linux.

You have to remove all lines from files __init__.py - they have to be empty.


But you show link to question which is 11 years old, and answer is 7 years old, and in current Python it may not be needed. You should rather show original problem because it may need totally different solution - like using relative paths, or adding script's folder to sys.path before import

furas
  • 134,197
  • 12
  • 106
  • 148
  • I appreciate the explanation. I am not too familiar with the Linux commands which is why I was confused on what was being written. After doing some more toubleshooting I ended up creating a subdirectory under Echo and placing an empty `__init__.py` and my python file that I wanted imported. Then in my `main.py`: `from lib import functions as func`. – Binx Aug 05 '21 at 14:35
  • I'll be sure to search for some more recent ways to import other python files. – Binx Aug 05 '21 at 14:38