We have a python 2.7 script that executes LOCALLY perfectly fine.
The problem occurs ONLY when I attempt to invoke the script remotely (SSH):
ssh user#server "python script.py"
*Traceback (most recent call last):
File "script.py", line 18, in <module>
import requests
ImportError: No module named requests*
After doing a little bit of research, I have tried manually inserting the absolute the path to the "requests" library using sys.path.insert before importing the library:
sys.path.insert(1,'/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg')
import requests
I even confirmed the path to the package got added succesfully by printing the sys.path when invoking the script remotely:
print '\n'.join(sys.path)
Result:
/usr/local/lib/python2.7/site-packages/setuptools-20.9.0-py2.7.egg
/usr/local/lib/python2.7/site-packages/requests-2.10.0-py2.7.egg
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
However, I am still getting the "ImportError: No module named requests" no matter what.
Can you help me understand what am I missing?
Again, this script does find the library without issue and runs perfectly fine when I execute the script locally.
Thanks!
requests is a non-standard Python library. Make sure it is installed on the remote box you are attempting to execute the script on.
On the remote, execute from the shell:
easy_install pip
pip install requests
If I understand correctly, you are trying to run the same script on the same machine, but once from a local shell and once from a remote shell (with ssh).
If this is the case, when you use the remote shell, be sure that you have the environment properly setup.
This may boil down to just setting the PYTHONPATH environment variable correctly.
In your case, this would be:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/:$PYTHONPATH
You may need to add more paths to it.
When I have this sort of issues, I usually run a python shell from the working environment and check where the files are taken from, so I can be sure the correct entries are set in the PYTHONPATH:
$ ipython
In [1]: import requests
In [2]: print requests.__file__
/usr/local/lib/python2.7/site-packages/requests/__init__.pyc
For this example, I would add /usr/local/lib/python2.7/site-packages/ to my PYTHONPATH if I was not able to import requests correctly.
Related
I use PyCharm Edu 2018.3, with a virtual environment path set outside my python application root folder (I use this environment for other python code).
When i run the program from PyCharm, it works without errors. But when i run it from a .bat file it fail to be able to import the packages from my environment.
Basically, it seems like all installed packages made in the environment, are not being found when launched from the .bat file.
So i was wondering if the fact that my environment was outside my root python code was an issue or if i had to precise a path, or else.
All packages imported are correctly inatlled in my envirnment and the code runs as expected when run in PyCharm.
Also, i tried to run a simple "Hello World' code from my .bat file and it works correctly, so issue only occurs when calling packages from my environment.
Could it be a setup file issue (i have none)?
Thank for the help and suggestions.
For instance, the import of selenium which is my first import, in the code, triggers the following error:
#
from selenium.webdriver.firefox.webelement import FirefoxWebElement as MyWeb
Element
ModuleNotFoundError: No module named 'selenium'
#
I'm guessing you have either installed selenium to the virtualenv that PyCharm setup for you when executing from withing the IDE. However When your running outside of the IDE you are using the default python interpreter not your virtualenv. To ensure your in the expected virtualenv make sure it's activated before running.
So for example
$ cd C:\Users\'Username'\venv\Scripts\
$ activate.bat
From this point you should be able to execute your bat script using that virtualenv.
If your still getting it with the virtualenv activated then try installing with pip while your virtualenv is activated.
First thing is that I'm not a python expert.
I have a project which were run on Mac by PyChart and was working well. Since I moved back to Windows I'd like to run my python project in Windows Linux Subsystem a.k.a. Ubuntu Bash command line. The reason behind are the following:
Ubuntu gives more flexibility to deal with different python versions
I connect to kerberized Impala where thrift-ssl and other packages are required and they are compiled by install time, on Windows I faced difficulties to install these. Ubuntu works like a charm. Due to this PyCharm on Windows is not an option
the service I connect to requires kerberos authentication. PyCharm running on Windows can manage spinning up Ubuntu and execute the developed script there, but kerberos (kinit) authentication gives extra complexity and requires me to put my domain password in a script or file. It's a no-go for me.
So, I moved my project to Ubuntu Bash. I created virtualenv, activated it and tried to run my script. It failed with the following error:
(venv) desktop#DESKTOP-ST034M2:/mnt/c/DEV/project$ ./etl/finance/exports/export.py
Traceback (most recent call last):
File "./etl/finance/exports/export.py", line 5, in <module>
from util import db_connection
ModuleNotFoundError: No module named 'util'
This is what project looks like:
project/
project/__init__.py
project/etl/
project/etl/__init__.py
project/etl/finance/
project/etl/finance/__init__.py
project/etl/finance/exports
project/etl/finance/exports/__init__.py
project/etl/finance/exports/export.py
project/util
project/util/__init__.py
project/util/db_connection.py
What export.py looks like:
#!/usr/bin/python3
import csv
import datetime
from util import db_connection
c = db_connection.Connect("finance_db")
Having __init__.py in every directory means that - at least for me -, python manages the directories as packages/modules, so I can include python files from different directories. So, my assumption is that if I execute the scripts from the project directory, which is the project root directory, then python should find the files in the directories.
What I'm doing wrong here? What other parameters should I setup and/or check to have the script running?
try
PYTHONPATH=. python youstartfile.py
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
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.
I'm trying to use this module in a project, but can't figure out how to use it.
What I've tried:
Downloaded the module as a ZIP file.
Unzipped that file, giving me the directory structure I saw in the GitHub page.
Opened up Terminal, and navigated to the directory with setup.py in it.
Ran the command sudo python setup.py install
This seemed to run fine, as I get a message saying "Finished processing dependencies"
Now, when I go into PyCharm (the IDE I'm using) and try to run import readability I get an error saying ImportError: No module named 'readability'
Possible reason for failure:
I specified to PyCharm that I am using a Python 3 interpreter. Would Terminal by default install in the 2.x Python directory?
Does the location of my PyCharm .py file matter?
My main error was running the python commands in Terminal under version 2.7.2. By using the keyword python3 instead of python, I was able to force Terminal to use the correct version. I also needed to install setuptools.