I want my RPi to open run a python webserver and open Chromium to access it.
It should do it automaticaly on startup.
BUT
When I run the script via command
$ sudo python3 /home/sps-training/python/webserver.py
and open localhost on chromium, it says that it can't access HTML file in another folder (/deploy)
but when I open the dictionary first
$ cd /home/sps-training/python/
and then open the script
$ python3 webserver.py
it suddenly works!
So there are 2 possible solutions
The first one is to make it work by using:
$ sudo python3 /home/sps-training/python/webserver.py
The second one is to automatically access directory and the start the script
Right now I'm using /etc/profile to run it on startup (I just wrote at the last line with & on the end of the line)
Thanks a bunch for every advice!
btw sorry for grammar
Like Barmar said. It sounds like the issue you're running into is a working directory vs. current directory problem. When you run sudo python3 /home/sps-training/python/webserver.py you are launching webserver.py in the directory / and if it looks for "deploy/index.html" it's going to look in /deploy/index.html. However, when you cd into /home/sps-training/python/ and then run python3 webserver.py the working directory is now /home/sps-training/python/ and it will look for deploy/index.html in /home/sps-training/python/deploy/index.html
The best solution is to edit the python script to use absolute file paths.
Barring that, you need to cd to the correct working directory every time.
Related
I am setting up a PC running Ubuntu 20.04.3 with a touchscreen and no keyboard/mouse. Due to this, I am putting desktop icons that will run the software needed for data taking/analysis. I was able to set one up with no problem first using a .sh file that executes with a single click, then by using a .desktop file.
The problem I am having is that this will not work with a python3 file that runs tkinter and ROOT. I started off by writing the shebang #!/usr/bin/env python3 at the beginning of the .py file and the .sh file that executes the python file. When I click on the .sh file, nothing happens. If I run the .sh file on the terminal by typing ./Bash_File.sh, it executes with no issues. Also, I did make the .py executable, so I can also run it by going to the directory and typing ./Python_File.py.
I then decided to try with a .desktop file, similar to the first program I got to work. I tried executing both, the .sh file and the .py file, from the .desktop file and I get the same error: ModuleNotFoundError: No Module named 'ROOT'. In the .desktop file, I execute the .py file using Exec=/filepath/Python_File.py. Again, I am able to run this file through the terminal, but not having any luck using the icons to run it. I've looked everywhere online for an answer, but no luck. Thanks for the help
Disclaimer: Generic answer as I know nothing about the ROOT module.
This is a python error, it can't find the ROOT module, mostly like a specific file named ROOT.py. The cause is that a Bash login session is different than a Bash system session.
Run this script from a desktop launcher and in the shell:
#!/usr/bin/env bash
echo $PATH | tr : \\n >my_path-`date +%s`
If the two output files are different, figure out which directory contains 'ROOT.py' and add this to your launcher script:
#!/usr/bin/env python3
import sys
sys.path.append('/the/missing/directory')
If this doesn't set you up right away, there are many articles on managing the Linux system path, the difference between a regular Bash shell and a Bash login shell, and how to manage your Python path. HTH
I am learning statistics and Python from this book called ThinkStats. It has instructions on how to download the code and solve the exercises. I did everything that was told but am still not able to run the code on jupyter notebook. I am not sure what I am missing. Can someone please help me? Here is a list of instructions in the book and the things I did till now.
"After you clone the repository or unzip the zip file, you should have a folder called ThinkStats2/code with a file called nsfg.py. If you run nsfg.py, it should read a data file, run some tests, and print a message like, “All tests passed.” If you get import errors, it probably means there are packages you need to install."
Till now I downloaded the zip file, unzipped it and got the file named nsfg.py. I opened the file in jupyter notebook, but I am getting an error called" cannot find module thinkstats2". This module is specific to the book and from what I understand, the module is in the Thinkstats2 directory. How do I make jupyter notebbok run the file in the ThinkStats 2 directory?
You can run nsfg.py using the Python interpreter in your terminal.
$ cd ~
$ git clone git#github.com:AllenDowney/ThinkStats2.git
$ cd ThinkStats2/code/
$ python3 nsfg.py
(13593, 244)
All tests passed.
Alternatively, you can launch Jupyter Notebook in a specific directory:
$ jupyter-notebook --notebook-dir=~/ThinkStats2/
If you navigate to http://localhost:8888/tree in your browser, you should see the contents of the ThinkStats2 directory. Then click "New" > "Python 3".
In the first cell block, enter cd code, and execute it. In the second cell block, enter %run nsfg.py. You should see the same message as with the Python interpreter.
Notice that you must be in the code/ directory to run the file because it uses relative file path references. This gets at a larger reason why I would not personally recommend learning Python with ThinkStats: it is statistically sound, but often it does not use the best Python practices and forms bad habit as a result. In other words, python ~/ThinkStats2/code/nsfg.py will raise a FileNotFoundError, which is easily avoidable.
When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)
I have a script/app/program written in python 3. I uploaded it to my Ubuntu box and changed the permission to allow execution by all. I am able to run python myapp.py with no problem but I cannot run myapp.py. I get an error that it is not a recognized command. I have at the top
#!/usr/bin/env python3
That should be right from all that I've read so far. I even tried
#!/usr/bin/python3
in the program referred to as myapp.py
Neither of them work.
I was following an online course and all was going well until we got to that point of running python scripts like regular programs by setting the execute setting.
If you are talking about, executing it from any directory, you need to do two things.
Setting the path variable. Lets say I need to execute Test.py, which is in Desktop, from any directory
export PATH=$PATH:/home/thefourtheye/Desktop/
Giving execute permission to the file
chmod 755 /home/thefourtheye/Desktop/Test.py
Then I can execute it by simply typing Test.py.
You cannot execute file in unix by name without directory name due to some security considerations, so you have to add . as directory (it will look like ./myapp.py)
. isn't on your path (and it probably shouldn't be for security reasons), so Ubuntu won't look in the current directory for a myapp.py program to run. You need to explicitly specify ./myapp.py to indicate that you want to run the myapp.py file in the current directory.
You should either call it as
./myapp.py
or the current directory should be in the PATH environment variable (either as full directory path name or as '.' which indicates the dynamic current path), check with
echo $PATH
to add it you can run
export PATH=$PATH:.
If you want to run it like:
user#machine:~$ myapp.py
You must put the script in the /usr/bin/ or /bin/ or something similar. In other case you must run like:
user#machine:~/appFolder/$ ./myapp.py
did you try:
$ sudo chmod a+x myapp.py
then run the python code using:
$ ./myapp.py
For linux users, may be text format dos/unix problem!
try,
sudo dos2unix name_of_your_script
then shebangs will probably get to work properly! That was what happened to me.
PS:
Ofcourse, also your script has to be executable
(sudo chmod +x name_of_your_script )
I have been putting my code on github, but I've run into an implementation snag. I run the same code on many computers (including a computer that I do not have root access on).
One piece of code (a bash script) calls some python code like:
python somecode.py
The shell will run the correct version of python, but it won't find somecode.py.
What I've tried:
Fail #1: I tried to add both the directory which contains somecode.py and the full path to the file to the PATH; to no avail. [Errno 2] No such file or directory
Fail #2: I can make it work for one computer ONLY if I add the full path to the correct version of python in the top line:
#!/usr/local/cool/python/version/location
However this breaks it running on any other computer.
Fail #3: I can also make it work if I make the bash script say:
python /full/path/to/github/place/somecode.py
but again, this only works for ONE computer because the paths are different for different computers.
What I really want to do: I want to be able to use the same code (both bash script and somecode.py) on multiple computers.
Any suggestions about how to do this properly is welcome. Thanks!
Solution
Added:
#!/usr/bin/env python
To the top of my somecode.py code;
mv somecode.py somecode
chmod +x somecode
Make sure PATH has /full/path/to/directory/with/somecode.
Bash script now says only:
somecode
and it works.
For problem #2 try
#!/usr/bin/env python
though it may find different versions of Python on different machines, but as long as that's not a problem this should fix that particular problem
See this SO question Python deployment and /usr/bin/env portability too. And this post by Alex Martelli re use of this.
If you say python somefile.py then it will take the location of somefile.py as the current directory, not from $PATH. It will take the location of python from $PATH.
If you say somefile.py then it will take the location of somefile.py from $PATH, and the location of python from the #! line of your python script, which can use the PATH if you follow #Levon's suggestion.