The code I wrote in VSCode does not work

beratwalshh

New Member
Joined
Sep 10, 2022
Messages
1
Reaction score
0
Credits
12
I am using ubuntu. I writing a python code but it doesn't work.
1662841609220.png

berat@berat-Latitude-E5470:~$ /bin/python3 "/home/berat/print("hello world").py"
/bin/python3: can't open file '/home/berat/print(hello': [Errno 2] No such file or directory

please help me, i am inexperienced in ubuntu.
 


The problem there is the filename you’ve chosen for your script. Because you’ve used some special characters in your file-name, you’ll need to escape them with backslashes.

E.g.
Bash:
python3 "/home/berat/print\(\"hello world\"\).py"

Personally, I’d recommend renaming the file to something like helloworld.py. Would make it a lot quicker to type in the terminal!

Then you could run your script like this:
Bash:
python3 ~/helloworld.py

Note ~ is a convenient shell shortcut that expands to the path to your home directory. Which in your case is /home/berat. So any time you need to type the path to your home directory, instead of manually typing the path, you can use ~/ instead.

Additionally, if you put a shebang line as the very first line in your Python scripts:
Python:
#!/usr/bin/env python3
print("hello world")

And then make your script executable using:
Bash:
chmod +x ~/helloworld.py

Now you can run your script directly, without having to specify Python3:
Bash:
~/helloworld.py

When you run your script, what happens is:-
Bash will read the first line of the script - the shebang line, which tells bash to run the file in python3.
The shell will then load and run your script in the Python3 interpreter.

When the Python3 interpreter runs your script, it will see the shebang line as a comment and will ignore it. So the shebang will not have any negative affect the rest of your script. It literally just tells the shell which interpreter/executable to run your script through.

I hope that all makes sense?!
If not, feel free to ask more questions!
 

Members online


Latest posts

Top