My python script is as below
#!~/PyEnv/bin/python
import sys
import my_lib
print 'hello'
# do something with my_lib
my_lib()
sys.exit(200)
I placed it in /csp folder with name 'hello.py'. When I connected to "localhost:8080/?hello.py", I received a message "ImportError: No module named my_lib".
Because this script didn't run with python in virtualenv. How can I resolve it ?
This should be a problem with the local path. VirtualEnv is just a setup tool that can generate local python environment. This is used a lot for isolating a project from the system python.
I think that when you use the path ~/PyEnv/bin/python, then this version of python doesn't automatically redirect import requests to ~/PyEnv/lib.
This is some PATH issue and I am not sure if G-WAN should necessarily address this :)
Let's try to narrow the cause of the problem:
Does the G-WAN Python example run fine WITHOUT virtualenv?
Does the G-WAN Python example run fine WITH virtualenv?
Does your script work without virtualenv?
If the latter is true then you might need to investigate what virtualenv is doing.
Related
I have a python Script when i run my script by using my account it's run without problem ,
But when i use the root account i have an issue like this
I don't know why i have a problem with import module
use an absolute python path to point to the version of python that has the right packages installed.
The command which python executed as the user where the script works should help.
If you used pip to install any package locally, then you might have to
ensure as well, that the environment variable $HOME points to /home/robot.
In general I'd recommend to create a virtualenv foe your specific python project and use it in the sudo call explicitely or if you refer change the #! line in your script to point to the right virtualenv,
When I try to run my program from the PyCharm IDE everything works fine but if I type in Fedora:
python myScript.py
in a shell prompt I get an import error from 1 of the module.
ImportError : No modue named myDependency
What does PyCharm do that allows the interpreter to find my dependencies when launched from the IDE? How can I get my script to find its dependencies so it can be launched with a singe command?
There are a few possible things that can be causing this:
The same python interpreter? Check with import sys; print(sys.executable)
Is it the same working directory? Check with import os; print(os.getcwd())
Discrepancies in sys.path, which is the list python searches sequentially for import locations, can possibly caused by environment variables. Check with import sys; print(sys.path).
Adding this worked for me:
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
As a first step, in PyCharm go to project settings / python interpreter, and note the path. Then compare that to the result of which python -- do they line up? If not, you need to make them do so.
If that's ok, check what PyCharm defines as your project root in project settings / project structure. Is that the directory where your script is located? If not, you should run the script from that directory or append the directory to the $PYTHONPATH variable.
Almost definitely, it's one of those two things.
You might have set some project dependency in Pycharm for module myDependency.
You can access the same in Fedora by importing the module explicitly or by creating the egg of that module and installing it.
This will then go to python site-packages from where you can refer this dependency.
im trying to test Machine Learning codes from this site :https://github.com/lyuboraykov/flight-genie
im really new to machine learning and im using windows.
i already installed the requirements to run the code (python, virtualenv, numpy, sklearn, scipy, etc) but i got stuck when i try to run the main code, it shows notification like this :
please help, thanks
If you run a python script as
python dir_name/script.py
... then python will execute the file named script.py inside the folder dir_name. What will happen here is python program will go inside dir_name folder first and then run the script.
In your case if you type python flight_genie/main.py, it will go inside the folder flight_genie and will execute the file. Then python can't find a folder (actually the module) named flight_genie there because python program is already inside that folder. That's why you get this error.
So one way of fixing this issue is replacing all import flight_genie.xxxx with just import xxxx. (Also from flight_genie.xxxx import yyyy with from xxxx import yyyy)
But it is so time consuming if you have a large project. (And sometime it won't even work). So best way is to run the project as a whole module.
If you look at here you can see how to run python modules as scripts. You just have to type the following command in console.
python -m flight_genie.main
ps: I assume that you have python3 installed in windows and configured to run python3 when you type python in command line.
Have you made installation steps described on author's page? I'm afraid that you didn't read this...
sh
# you have to have python 3 installed
pyenv env
source env/bin/activate
pip install -r requirements.txt
python flight_genie/main.py
Based on the error message I think your problem is with your import. I'm guessing it should be something like:
from flight import Flight
Instead of:
from flight_genie.flight import Flight
If that doesn't work try to post your code so we can try to find the problem.
For activation there is a script that activates a virtualenv from an already running python interpeter using execfile('C:/path/to/virtualev/Scripts/activate_this.py', dict(__file__='C:/path/to/virtualev/Scripts/activate_this.py')). However since I can still import packages that are not in the virtualenv from the current python script I am confused about how it works.
For deactivation there is no python script at all.
What should I do?
From part of the VirtualEnv homepage.
You must use the custom Python interpreter to install libraries. But
to use libraries, you just have to be sure the path is correct. A
script is available to correct the path. You can setup the environment
like:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
If you want to run a program outside of the virtualenv, just run your system python executable (e.g. /usr/bin/python) instead of the one in the virtualenv.
This souds like bad idea. You are trying to modify environment of your script within this script. Please explain why?
Can't you do it hierarchical? Use one script to run different scripts in different virtualenvs.
at the command line, type the word 'deactivate'
I have no idea what could be the problem here:
I have some modules from Biopython which I can import easily when using the interactive prompt or executing python scripts via the command-line.
The problem is, when I try and import the same biopython modules in a web-executable cgi script, I get a "Import Error"
: No
module named Bio
Any ideas here?
Here are a couple of possibilities:
Apache (on Unix) generally runs as a different user, and with a different environment, to python from the command line. Try making a small script that just prints out sys.version and sys.prefix, and compare the result through apache and via the command line, to make sure that you're running from the same installation of python in both environments.
Is Biopython installed under your home directory, or only readable just for your normal user? Again, because apache generally runs as a different user, perhaps you don't have access to that location, so can't import it.
Can you try doing import site before trying to import Biopython? Perhaps something is preventing site packages from being imported when you run through apache.
In the cgi script, you could try to add the path to this package before any import.
sys.path.insert(0, 'path to biopython package')
If you are using Apache, you should be able to set the PYTHONPATH in conf file with directive SetEnv
SetEnv PYTHONPATH "path to biopython package"
I had same problem. I solved this problem by changing user of Apache in Linux Ubuntu by command in terminal:
sudo gedit /etc/apache2/envvars
Please change www-data on export APACHE_RUN_USER and export APACHE_RUN_GROUP to your current user or user that can run python script.
Have a good time ;)