Cant run Machine Learning codes with virtual environment - python

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.

Related

import error that doesn't replicate in different clones of the same repository

I have a big python project. When I try to execute a certain script I get an import error
something like:
from my_package import xyz
ImportError: cannot import name 'xyz'
Where my_package is an existing directory in the code base, and xyz.py is within this directory. Yes, I checked, and this directory is in the search path.
What really boggles my mind is that on a different machine, when I clone the same repository, activate the same virtual environment, and try the exact same script - I don't get this error.
I try to figure what is wrong and how to fix it, and the fact that it happens on a remote server, and does not replicate on my own computer just seem weird.
Any clue what could go wrong and where should I look for the bug?
EDIT: some additional information -
When I ran the script as it should i.e.
python ./myscript.py
It gives the above error. But when I run the problematic import command within the interpreter:
python
from my_package import xyz
I get no error whatsoever.
The three potential errors I can think of are the following:
Does the git project use submodules? If so, you need to make sure you run a git clone with the --recursive flag, since it might not have downloaded all the submodules correctly.
How are you setting up the virtual environments? Usually they shouldn't be stored within the git repository, but should instead be created on each machine. The dependencies can then be installed from the project's requirements.txt file. Sharing the same virtual environment between multiple machines could mean that it doesn't behave correctly on one of them.
Have you compared the versions of Python used between both machines. If it's a built-in module that's failing to import then perhaps your second machine isn't using a new enough version of Python.

Run python Script as root

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,

Python standalone .py or external import of module? Is this really as hard as it seems?

Situation: I've installed a bunch of packages with pip. I've now written code using these packages.
I have a myscript.py
My friend is on windows.
has python installed.
He has no packages.
He cannot get any packages.
He has pip
He will never be able to use the internet to get more packages everything must be hand delivered.
In fact about 10 minutes after he runs whatever I give him, he formats his machine and it's gone.
How do I take myscript.py and give it to him on a USB stick so that he can copy the file myscript.py onto his computer and run it?
I thought Pipenv would do it but it looks like it just creates a LIST of packages to download from the internet. (a very well defined list... but a list not the actual files needed to run something. Do I understand it correctly?
Right now I'm giving him .exe made with py2exe. This isn't very elegant considering he has python already.
tl;dr how do I give a python script .py to an end user that doesn't have the internet?
You can package everything up in a virtual environment and give him the complete environment necessary to run the script. You can read about this at https://developer.mozilla.org/en-US/docs/Python/Virtualenv and an IDE like PyCharm will help you create such an environment easily.
It's actually a good thing to learn about and do anyway.

Script working in ipython but not from the command line

I have a script that functions from within ipython but when I try and run the same script from the command line I receive import errors for a local module that I am trying to import:
from helper_functions.email_from_server import send_email
Error:
ImportError: No module named helper_functions.email_from_server
This script imports from within Ipython without any issues.
Comparatively, I have code that runs without any issues within ipython I can run another script using the command:
run script.py
From the command line I can run the same script:
python /dir/script.py
However this python /dir/script.py doesn't work with the script with local imports (from above) and I can't figure out if its a pythonpath issue or some local env issue? I have been reading through stack to find it but haven't been able to thus far. It feels like its just around the corner
One attempted solution:
PYTHONPATH=/dir/ python /dir/script.py
EDIT (to help clarify):
I am using an anaconda distribution on a linux machine.
Mucking about with PYTHONPATH is a recipe for sadness. You can do it, but you shouldn't. The correct thing to do is install your package in your correct environment. If you don't know how to create a package here's a super simple example. There may be some differences in your path when running via ipython vs command line.
You can find out what the differences are by using sys.executable and sys.path:
import sys
print(sys.executable)
print(sys.path)
Run that from IPython, and then run that from the python on your command line. You will undoubtedly get two different results. Since you're running Anaconda, you want to follow their guide for installing non-conda packages to install the one that you build.
Though of course that assumes that you've got the anaconda python on your path - you can check that out with which python since you're on Linux.
I resolved it via creating a wrapper shell script. Ugly in that i'm exporting the python path each time, but it works.
#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/my/dir"
source ~/.bash_profile
cd /my/dir && my/anaconda/location/bin/python /my/dir/to/script/cript.py

How to run gwan with python in virtual environment?

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.

Categories