Hi I'm running a python script that transitions tickets from "pending build" to "in test" in Jira. I've ran it on my local machine (Mac OS X) and it works perfectly but when I try to include it as a build task in my bamboo deployment, I get the error
"from jira import JIRA
ImportError: No module named jira"
I'm calling the python file from a script task like the following "python myFile.py" and then I supply the location to the myFile.py in the working subdirectory field. I don't think that is a problem because the error shows that it is finding my script fine. I've checked multiple times and the jira package is in site-packages and is in the path. I installed using pip and am running python 2.7.8. The OS is SuSE on our server
That is very hard to understand what you problem is. From what I understood you are saying that when you run your module as standalone file, everything works, but when you imoprt it you get an error. Here are some steps towards solving the problem.
Make sure that your script is in Python package. In order to do that, verify that there is (usually) empty __init__.py file in the same directory where the package is located.
Make sure that your script does not import something else in the block that gets executed only when you run the file as script (if __name__ == "__main__")
Make sure that the python path includes your package and visible to the script (you can do this by running print os.environ['PYTHONPATH'].split(os.pathsep)
Confirm that you don't have another file or directory that shares the same name as the module you are trying to import.
Related
I've run into a strange error I can't quite figure out involving a library installed via pip.
I ran pip3 install mcp9600 to install a library for a temperature sensor, and then verified that this worked as expected by running a python terminal, importing the library, and invoking the relevant class:
After that I tried to run the exact same thing from a .py file and got the error below:
I'm really not quite sure what is happening or how to fix it, since I've never had this issue before. What's going on here?
Your script is named mcp9600.py so the code import mcp9600 imports the script as a module. The script (imported as a module) doesn't have any MCP9600 hence AttributeError.
Rename the script to something else. Even mcp9600 (without .py) is ok. Take the lesson: never name your scripts the same name as Python libraries; for example, never name your scripts email.py or test.py — they overshadow Python's email and test.
your script file is also named mcp9600.py, just like the module. Python begins searching for modules in the same directory as the source file, so it finds the script itself instead of the module.
Try renaming your script.
I figured it out. Seems like I can't call the python script the same as the library name!
I'm new to this but embarrassed all the same!
I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).
I used PyCharm (community edition) to write the script and it is working from inside the IDE.
I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:
from package_name,file_name import ClassName
The Error message in the command line:
ImportError: No module named 'package_name'
if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.
What worked fine for me is:
Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.
for example ,I was running the script file from conf path and my script was importing module situated in \scripts\Setup\ so appended the path of module like below.
import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup')
then use import statement of module for which issue was thrown.
I found the answer for my question above, and the problem was much easier than I thought.
Addressing the Problem
having many python packages in different directories
your script needs some/all packages, that are not in the standard lib-directory for your python installation (e.g.:prefix/lib/pythonVersion).
Solution
Short term solution
As long you are using an IDE (e.g. PyCharm), it is sufficient within the code to add:
import sys
sys.path.append("path/to/package")
As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.
Better solution
Add the directories of your packages and of your python installation to your shell-profile(e.g.: .bashrc) using the command:
export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages
To get more info about PYTHONPATH, check this link
In this case you will not need to append the path of your packages within your code :)
I installed WinPython-64bit-3.5.1.2
I installed it not into default directory C:\Python, but into a different one like C:\"....My path...."\WinPython-64bit-3.5.1.2.
I downloaded a specific python project with all of its files and modules and placed this folder in Python folder so it has path like: C:\"....My path...."\WinPython-64bit-3.5.1.2\project
I added this path to the system path like this:
import sys
sys.path.append("C:\"....My path...."\WinPython-64bit-3.5.1.2\project")
There is an setup.py file in the project folder which I can't seem to run - whenever i type python setup.py I get an error: SyntaxError: invalid syntax
Whenever I try to import some module from this project I get the message ImportError: No module named %module_name%
I tried this both from Spyder and Ipython console.
I've read some stuff about system variables and their altering, however I do not seem to have "pythonpath" variable defined, do i need to define it? What do I put in it?
Its weird, because yesterday I was able to import the exact same module, but today I already can't. I couldn't find out what is the problem here and don't seem to remeber the exact steps I performed the day before. So am asking for qualified help. This is my first time using Python.
Thanks!
suppose there is a Project\setup.py
launch WinPython Command Prompt.exe
cd/D "C:\"....My path...."\WinPython-64bit-3.5.1.2\project"
python setup.py install
and, then, to check it's there:
pip list
First, I've found how to call a script from within an other script in Python, the call works perfectly well, but here's the problem I'm running into :
In order to easy-install my web-app (Bottle) on an another server, I packed inside a /redist rep, with mod_wsgi and PyMySQL source files. What I'm trying to achieve is a kind of "setup.py" file, that will launch the /mod_wsgi/setup.py install file and the same with the PyMySQL setup file.
Here's what I'm doing for PyMySQL for example :
subprocess.call("python3 ./redist/PyMySQL/setup.py install", shell=True)
The instalation runs fine, BUT, I end up with a /build, a /dist and a /PyMySQL.egg-info folders in my app directory, and when I'm trying to launch anything that imports PyMySQL, it told me that the module didn't exist.
If I manually install it (using my terminal I mean, like cd /redist/PyMySQL/ and then py3 setup.py install), it works great, and the import will then work ...
Any idea ? Am I doing something wrong ?
By advance, thanks :)
I think this would solve your issue : Python specify popen working directory via argument
I suppose in the "./redist/PyMySQL/" directory could be used as parameter because it is where the setup.py is located
try this :
subprocess.Popen("py3 setup.py", cwd='/redist/PyMySQL/')
on my end this works :
subprocess.Popen(['py3','setup.py'], cwd='path-of-my-setup')
I have a simple .py file that queries a DB server with pymssql that works as required.
I am trying to schedule a task to run the script but it fails
When I call the .py file from a batch file I get the below error:
import pymssql
ImportError: No module named pymssql
Any ideas why this is failing when run from a batch file ?
Python 2.7.6
Running on Windows 2008 R2
OK, so it turned out that the Python.exe I used when testing the script was in C:\Python27(32 bit) and not C:\Program Files\Python27 (64bit)
I needed to specify the correct exe and it worked.
thanks to those who contributed
I faced similar issues however I was running on Raspberry Pi. Had trouble running them from etc/rc.local and it kept showing me the error.
What I did
Created print_path.py in the directory of the python script you wanted to run and write the following:
import sys
print(sys.path)
Go to terminal
Make use of this line which i took from here:
sudo python3 [path_of_file]/[nameoffile].py > [path_of_file]/output1.txt
Go to terminal
sudo nano etc/rc.local
Place the following into rc.local:
sudo python3 [path_of_file]/[nameoffile].py > [path_of_file]/output2.txt &
** Take note of the Ampersand
Compare the two outputs
You should be able to find the path that differs.
Then copy the missing path.
Put into python script(that you want to run on boot)
import sys
sys.path.append('/home/pi/.local/lib/python3.5/site-packages')
It should be working because it works for me.
try adding the full path of the module (like this "C:\myfolder\mymodule.py") to the %path% variable within the batch file and then executing it
PS:
Set %path% = %path%+"C:\myfolder\mymodule.py"
should look something like this