I wrote myself a small Python script that I want to use to automatically do things with certain types of files; as such, I want to create an .app out of it so that I can set certain files to be opened with it automatically.
So I looked around and found Platypus which seems to do what I need.
However, weirdly it doesn't work. Specifically, it does not seem to be finding the right python interpreter. I set it up as follows:
I.e., the script type is env so it should just read the top line of the file like the shell does.
In magic.py, the top line is #!/usr/bin/env python2.7.
Now, when I run the shell script on the command line (that is, ~/devel/magic.py whatever), everything works fine. But when I run the app, it errors with:
Traceback (most recent call last):
File "/Users/jan/Dropbox/devel/Magic.app/Contents/Resources/script", line 8, in <module>
from bencode import *
ImportError: No module named bencode
The same import works just fine when running it from the command line, so I'm thinking it's using the wrong interpreter somehow. How can I fix or debug this?
You are trying to import from bencode module but you didn't add it in the application's bundled resources. Either drag it to the list of included files and export again or just copy it to the resources folder in the package's contents.
Related
I have an executable python script which archives data from mysql server using the pymysql library. The script works well from the command line.
I call this script from a php script using escapeshellcmd function and I've gotten it to work.
I also have created a bash script that I intend to use from crontab to archive the information as well. I can make this script work as well, by making changes outlined below.
Somehow I have gotten into python versions and path problems.
if I include
#!/home/tim/anaconda3/bin/python
as the first line of the python script it works when called by the php script (using www-data as the user, I believe). It doesn't work from the bash script or the command line, giving the following error:
File "./signal_archive.py", line 22, in <module>
import pymysql
ModuleNotFoundError: No module named 'pymysql'
However, if the first line of the python script is as follows:
#!/usr/bin/python3
the script works from the bash script and the command line but not from the php script. It gives the following error:
File "/home/tim/python/commodities_related/signal_archive.py", line 23, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Both packages are installed on my system. Thinking pointing the script to the path would help, I added the following to the python script but no luck so far.
sys.path.append('/usr/lib/python3/dist-packages:')
sys.path.append('/usr/local/lib/python3.5/dist-packages:')
There is obviously something I'm missing; I think it is that php script is called by www-user and I don't know the default path. The bash file is called by my user with the path specified in the .bashrc file. However, I may need to point the apache or php (www-user) to use a specific installation of python.
EDIT-
To be more clear, a php script (phpfile1.php) calls the python script. When I call phpfile1.php from another php script (phpfile2.php) running on apache2 I everything works using the
#!/home/tim/anaconda3/bin/python
When I call the same file (phpfile1.php) from a different php script (phpfile3.php) from a bash script it fails.
Additionally, if I run the file in place using the following
./signal_archive.py
I get the error but if I run it using the following command it works:
python signal_archive.py
Any ideas if this is right or how to do it? Thanks.
I fixed this in 2 steps:
It turns out that I needed to add the path to anaconda3 to my .bash_profile file.
export PATH="/home/tim/anaconda3/bin:$PATH"
When anaconda3 is installed it modifies the .bashrc file with the previous code snippet. However .bash_profile made the difference in this case.
I also modified the top of the python file to use the anaconda path for execution, as well as add the path for the specific python packages.
#!/home/tim/anaconda3/bin/python
import sys
sys.path.insert(1, '/usr/local/lib/python3.5/dist-packages')
I am pretty new to python, interpreters, and programming in general. I script my .py files in pycharm. As you probably know pycharm copies the default interpreter to every project. When I install external libraries I install them at the project interpreter, not at the main interpreter. That works well during the pycharm development phase, but when I try to run the script outside it, it runs it with the default interpreter ( without the newly downloaded libraries ). How can I change that every script uses his own interpreter?
I don't want to download all these libraries to the default compiler and I don't want to change the default library every time I run another script ( via variable editor ).
Also I don't want ro run all my files using pycharm everytime ( since it's quite resource consuming and it takes a while )
I haven't tried anything yet, I couldn't find info about this.
When running without pycharm:
Traceback (most recent call last):
File "C:\Users\Andrei\PycharmProjects\mcdis\mcdis.py", line 1, in <module>
import discord
ModuleNotFoundError: No module named 'discord'
PS: The OS is Windows 10.
I found a shortcut:
Using a shebang ( a line of 'code' that specifies the interpreter which should be used ). While virtual environments seem to have some advantages the shebang trick should do it for most of the scripts.
How to use a shebang :
#!"C:\Users\Andrei\Desktop\MCSERVER\venv\Scripts\python.exe"
Just tell the path to the interpreter with a '#!' in front. Not sure if it works with relative path but I think it does.
I know there are similar questions, but I've looked at them and none of them are what I'm looking for
I'm running Python 3.7.0 on Windows 10, and I installed pygame-1.9.4 a few weeks ago, and it's been working fine until yesterday.
I have a file called testing.py that just contains:
import pygame
when I run it via
C:\Users\Me>testing.py, it returns the following error:
Traceback (most recent call last):
File "C:\Users\Me\testing.py", line 1, in <module>
import pygame
ImportError: No module named pygame
But when I'm in the Python shell, I can import it just fine
This is the case for every third party library. I have tested Pillow, flask, and others. Importing from the standard library works fine
I have uninstalled and reinstalled python and pygame, I've updated pip, I've updated setuptools as one person suggested, but none of it works
I had been using Wing IDE personal 6, and I could also run it fine from there, but I didn't care for it. Please help!
I am guessing that there are multiple python versions on your system and the one associated with .py files is not the one that you are using in the other cases.
To debug, what you can do is:
Write in a .py file
import sys
print(sys.executable)
and also execute those lines from your python shell. That will print the path to the python interpreter that is executing said commands. The result will probably be different in your case.
What you can also do is check if
print(sys.path)
results in the same list using both methods, as it contains the folders being searched when importing
I want to edit my code in notepad++ and be able to run it from there. However as my script imports library from it's own folder it's proving difficult.
This is what the directory for the script look like
\test\test1.py\selenuim\webdriver\firefox etc.
When run by double clicking the file in the explorer it works. When run from notepad using all the methods I could find, it says
Traceback (most recent call last):
File "D:\test\test1.py", line 1, in <module>
import selenium.webdriver;
ModuleNotFoundError: No module named 'selenium'
I've already scoured this link: How to Execute a Python File in Notepad ++?
And there is no way to do the way I have in mind. Some include cd in them, however this did not help me.
I always use PyNPP plugin for that purpose. I also include the path of my scripts by setting PYTHONPATH. So for instance, if your script resides in C:\test then execute this code in cmd: set PYTHONPATH=c:\test this way Python will be able to locate the location of your scripts.
Once you install PyNPP it's dead easy to use, but make sure you set the right directory path of Python for the plugin from the drop-down menu: plugins -> PyNPP -> options and then select the right path mine is C:\Python27. PyNPP seems to execute Python from the script's location, this eliminates the need to set PYTHONPATH
I am running Python 3 via Anaconda on Windows 10. I am having trouble importing my own modules into Jupyter workbooks. E.g., if I try import fibo for a module named 'fibo.py' I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-4105c89a6fa8> in <module>()
----> 1 import fibo
ImportError: No module named 'fibo'
I have tried three things:
1) using sys.path.append("path\to\fibo_folder")
2) Changing the PATH and PYTHONPATH environment variables to include "path\to\fibo_folder"
3. Navigating the Jupyter Notebook to the same directory as 'fibo' is installed in.
Note that I have also included an empty .py file in the same directory as fibo called 'init.py'
Only 3) seems to work, but this is impractical if I am using modules stored in different folders (they need to be kept separate). I am sure 1) and 2) should work, but they don't seem to.
How may I import my own modules into Jupyter?
sys.path.append("/path/to/fibo file") should have solved the issue. (Note that in your question you have used "\" while giving the path, which is an escape character and is wrong and it should be "/". Not sure whether it is a typo in the question, but just mentioning it for the sake of completion.)
But like you mentioned in the question that sys.path.append() did not work, here is one common place where you might have gone wrong.
In the respective code (.py or .ipynb) where you need to import fibo, you should run the sys.path.append("/path/to/fibo file") every time you run the respective .py or .ipynb.
Meaning, it is not like you open the terminal, run python command, and type sys.path.append("/path/to/fibo file") in the terminal, close it and then run your .py or .ipynb file. This will not work and will throw the above-said error in your question.
sys.path.append() is a session variable and should be run every time you run your respective code in that particular .py or .ipynb file.
This should get
1) and 2)
working!
(Also, (I know, trivial) re-check the path to the "fibo" file is correct)