Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 no such file or directory

I am trying to make python3 executable scripts and run them from shell.I have python 3.4.0 installed on my system. So, I added '/home/spandan/python_codes' directory to PYTHONPATH, as I am planning to keep my scripts and modules here.

However, while trying to execute these, the above error is thrown by the system, and the scripts wont execute unless I go into the python_codes directory and then execute them.

Executing python program : Here I found out that PYTHONPATH is irrelevant while making scripts, and also how to set the python shebang. So I set mine as #!/usr/bin/env python3.4.0

Is it correct?

like image 790
Spandan Chatterjee Avatar asked Nov 19 '14 03:11

Spandan Chatterjee


People also ask

How do I fix Python No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

Why is it saying no such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.

What is usr bin ENV python3?

A shebang line #!/usr/bin/python3Its purpose is to define the location of the interpreter. By adding the line #!/usr/bin/python3 on the top of the script, we can run the file.py on a Unix system and automatically will understand that this is a python script. Alternative, you could run the script as python3 file.py .


1 Answers

You don't have to put your python codes in a global path. Just make your python 3.4 interpreter interpreter available globally. For that, edit .bash_profile or .bashrc file in your home directory and add the following line:

export PATH=${PATH}:/usr/bin/python3

That will make python3 executable irrespective of your current working directory. In order to execute code from your codes directory, you just have to write:

$ python3 ./your_code.py

Another way is to add the shebang at the top of your code as

#/usr/bin/python3

and change the permission to executable by the current user (by default it will not have execute permission).

$ chmod 744 your_code.py

and then executing the script directly as

$ your_code.py

I hope I could address your problem.

like image 187
Marty Avatar answered Nov 05 '22 18:11

Marty