When I launch my CGI (common gateway interface) by using this code:
# -*- coding: utf-8 -*-
import cgi
import os
import json
import cgitb
cgitb.enable()
from sklearn.feature_extraction.text import CountVectorizer
print 'Content-type: text/html'
print
print '<html><head><title>Interface Ticket Recognition</title></head><body>'
formulaire = cgi.FieldStorage()
os.chdir("C:\Users\iyacine\Desktop")
path= os.getcwd()
from subprocess import call
call(["Python", "applicationV1.py"])
if path=="C:\Users\iyacine\Desktop":
print '''changer'''
print '</body></html>'
I got an error:
I try to change the path in environment variable but this work only in cmd.
I installed anaconda i my computer and i can run the module sklearn.feature_extraction.text in Spyder but not from Apache. Do you know how to solve this issue please?
You should have a first line like #! /usr/bin/python (called Shebang line) in your script. You have to change that line to point to the python executable you actually want. For Windows have a look at the docs but there also is the possibility to run ".bat" files via CGI so you could use a file (eg runmyscript.bat) containing just
#C:\path\to\anaconda\python.exe path\to\script.py
which is a hacky workaround (I think) but should work, too.
In my experience it is the best to use virtualenv if possible - this does not fix the path-problem directly but helps keeping different applications isolated.
Related
I'm trying to import the file File.py file from a specific directory, C:\MyPythonFiles. This is what I tried(among several other different attempts):
import sys
sys.path.append("c:\MyPythonFiles")
import File.py
Can someone please tell me how to import File.py from the diretory C:\MyPythonFiles ?
Removing the .py so that its 'import File' didn't work either:
[UPDATE]
So in my File.py, I had an import for a file that I did not include in c:\MyPythonFiles, so that was a huge problem.
I edited File.py so it only has one function, printHello(). The script works, but PyCharm still does not recognize 'File' for import. I can still use the line File.printHello(), but it's annoying since PyCharm is not autofilling the functions for 'import File'. Any ideas on how to solve this new problem?
[UPDATE]
I think #alex answered the original question, and my PyCharm issue should be a seperate question. Thanks for the help #alex!
import sys
sys.path.append("c:\\MyPythonFiles")
import File
or
import sys
sys.path.append(r"c:\MyPythonFiles")
import File
or
import sys
sys.path.append("c:/MyPythonFiles")
import File
1.Don't need ".py"
2.path link error
Windows path in Python
My problem is very similar to the one here (yes, I know, sorry), but none of the suggested solutions have any effect.
I am trying to use a python3 module located in the same directory as the main program. I am launching from within it, and even though I have also added it to the sys.path (using this for help).
>>> import sys
>>> sys.path.append("/home/name/Documents/PythonFiles")
I still receive an ImportError no matter what I do. This has remained true both for my actual program and a simplified version using "hello.py3" and "test.py3" identical to the ones used in the earlier question (see below).
hello.py3:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print('HelloWorld!')
test.py3:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
import os
print(os.path.abspath(__file__))
from hello import hello1
hello1()
This is the output of running test.py3:
/home/name/Documents/PythonFiles/test.py3
Traceback (most recent call last):
File "trial.py3", line 7, in <module>
from hello import hello1
ImportError: No module named 'hello'
All the sources I have checked say that as long as either the directories of the files are the same or if the sys.path directs to their directory, the modules should import without issue, but then again I haven't used modules before so I'm open to the fact that I missed something basic.
Either way I apologize if this really seems like a duplicate question, I am at a loss for what to do here. What should I try next?
Being script1.py:
import cgi
import cgitb
print("Content-type:text/html\r\n\r\n")
print("hello")
and script2.py:
import cgi
import cgitb
import pymysql
print("Content-type:text/html\r\n\r\n")
print("hello")
In script1.py everything works fine (prints 'hello'), but script2.py returns a 500 error.
Considerations:
pymysql is installed and the proof is that it can be imported from the python interactive shell without trhowing any exception
both files have the same permissions
Then, why script2.py doesn't work?
You might consider if your script is being run by the web server (as opposed to your user shell session) in a different virtual environment, possibly one that doesn't have pymysql installed. If that's the case, you'll need to activate that virtual environment and install pymysql there.
To verify this is an instance of the library not being installed, you might try something like this in your script...
import cgi
import cgitb
import sys
print("Content-type:text/html\r\n\r\n")
print("hello")
if 'pymysql' in sys.modules:
print 'pymysql is installed'
else:
print 'pymysql is NOT installed'
Beyond this, checking your web server logs might provide more insight into why the 500 error is getting raised by the web server...
I am having issues with a program I am forking (Pinguy Builder) Right now I just want to point this program to load a different gtk.glade skeleton/bare code which seems to be a glade project script. I don't know much about python. However I know few commands but how would I point this from /usr/share/PinguyBuiler to /usr/share/Flamesbuilder ? (Given the code below) Thanks. I don't want this project going away and I know a bit of bash to get through this as I plan on forking this bash and python scripted program.
ERROR CODE:
#! /usr/bin/python
# -*- coding: utf-8 -*-
CODE:
im#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
from PinguyBuildergtk import PinguyBuilder_gtk
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.realpath(FlamesBuilder_gtk.__file__)))
#os.system('chmod a+x %s' % FlamesBuilder_gtk.__file__)
ERROR MESSAGE:
Traceback (most recent call last):
File "/home/flames/Documents/coding/flamesbuilder/flames builder/bin/PinguyBuilder-gtk", line 5, in <module>
from FlamesBuildergtk import FlamesBuilder_gtk
ImportError: No module named FlamesBuildergtk
note: the file will be pointing to usr/share/FlamesBuilder or something like that. I know how the structurer works I just cant get it to point without cd. How would I fix this error? Thanks. - Flames
I have installed pymindwave in the following directory:
C:\Python27\Lib\site-packages
However when I run another code which depends on it it says it can't find pymindwave. What's the solution?
Here's the first few lines of the code that gives the error:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import platform
import sys, time
from pymindwave import headset
import httplib, urllib, urllib2
import json
And I have taken the code and python_mindwave from these links.
P.S.: I am not a Windows person but MindWave doesn't get recognized in OSX so I am forcefully using Windows 8.1 enterprise edition!
Try importing mindwave instead of pymindwave.