I create a Python project using Flask and it works perfectly.
Then I upload it to my private Azure repository.
After that, another coworker clone the project and try to run in his machine, but it didn't work. He got the following error:
File "run.py", line 1, in <module>
from api import app
File "E:\PUC_Conexao_Colaborativa_API\api\__init__.py", line 7, in <module>
from api.instance.config import app_config, ambiente
ModuleNotFoundError: No module named 'api.instance'
I look on Stackoverflow and found this post:
Python error "ImportError: No module named"
One of the answer say that:
You edited init.py on windows.
The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
I remember when I install "Git for Windows" exe, it asks for how I want to config the line ending conversions and I select "Checkout Windows-style, commit Unix-style line endings".
Is this the cause that my Python project fails to run in another machines?
The project runs fine on my machine and I copy the entire project and give to the another coworker using a pendrive. When the coworker run the project using the files in the pendrive, it runs perfectly, just like in my machine.
But if I or anyone else try to run the project downloading it from the repository, if fails to run with the error that I mention above.
I suppose the error is caused by the conversion of Git when I push the project to the repo. If am I correct, how can I fix that?
Related
I am working on a cli application in someways similar to aws-cli.
I started my project with all my code in one big python file. it seems a bit disorganize so I broke up the code into a folder somethingcli similar to awscli.
I also made a directory bin/ with similar code that you find in aws-cli.
But now I am unable to run it.
I have the similar error when I try to run aws-cli.
So I would do: python3 ./bin/aws
Then get:
Traceback (most recent call last):
File "/home/user/Projects/Other/aws-cli/./bin/aws", line 19, in <module>
import awscli.clidriver
ModuleNotFoundError: No module named 'awscli'
I understand the error... But how is one supposed to run the code when you are developing? Are you supposed to run ./setup.py each time? Confusing.
So I run the install and then go to wherever it installed everything and then work on the code there and copy it back in the directory to then push it to my repo?
Also when I run the unit-tests for aws-cli I get a bunch of errors.
I am still new to Python so this might be a very stupid question. I am just trying to do things the right way.
Python version: Python 3.9.13
I'm trying to convert my python file to an executable using PyInstaller. The program uses the Google Cloud Translate API to translate given text between languages. When running python quicktrans.py in the terminal, the program works fine. Then I ran pyinstaller quicktrans.py, SHIFT + right-clicked the directory the executable was in, and ran the .exe file in the terminal. This is the traceback that it spit out (Note this is not the whole traceback because it is a little lengthy):
File "c:\users\kalab\realpython\quicktrans\google\cloud\connection.py", line 31, in <module>
get_distribution('google-cloud-core').version)
File "site-packages\pkg_resources\__init__.py", line 559, in get_distribution
File "site-packages\pkg_resources\__init__.py", line 433, in get_provider
File "site-packages\pkg_resources\__init__.py", line 970, in require
File "site-packages\pkg_resources\__init__.py", line 856, in resolve
pkg_resources.DistributionNotFound: The 'google-cloud-core' distribution was not found and is required by the application
Failed to execute script quicktrans
I've tried looking into this and some reason it's giving me a pip-like error. I've been trying to fix this for hours and no luck.
Note: To install its client library, as per the documentation, you must run pip install --upgrade google-cloud-translate
I'm thinking this might have something to do with this because the last application I used dealt with the Facebook client module and you only had to do pip install facebook-sdk and the executable made by PyInstaller ran with no issues.
If you want to examine my code used in my program, it's hosted on my GitHub.
Thanks to anyone helping me out here!
It is basically package building name issue. Pyinstaller tries to import
google.cloud
where Google cloud package is now called
gcloud
. So you need to create a hook file for that names
C:\Users\\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\PyInstaller\hooks\hook-gcloud.py
File contents:
from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('gcloud')
In my experience base on the helps in https://github.com/GoogleCloudPlatform/google-cloud-python/issues/1187 :
Go \Anaconda3\Lib\site-packages\PyInstaller\hooks folder (if you use anaconda otherwise you need to find it under python folder)
Find the hook-google-cloud.py (If exist, otherwise you need to creat the hook.
Write to existing code as shown below
'''
Copyright (c) 2017, PyInstaller Development Team.
Distributed under the terms of the GNU General Public License with exception
for distributing bootloader.
The full license is in the file COPYING.txt, distributed with this software.
'''
from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('google-cloud-core')
datas += copy_metadata('google-cloud-translate')
datas += copy_metadata('google-api-core')
Hope you find this explaination helpful. Thank you.
Alternate hook tweak
I'm running into this same essential problem with the Google speech engine.
It's odd how everyone here seems to have success with slightly alternate solutions to this. I really don't understand how the "patches" to the hook which leave copy_metadata('google-cloud-core') in place can work? The error thrown back reads The 'google-cloud-core' distribution was not found..., so how can one execute that line as is?
This is my replacement for the file content of hook-google.cloud.py, in order to build an exe using google speech:
# PATCH: PROVIDED ALTERNATE PACKAGE NAME
from PyInstaller.utils.hooks import copy_metadata
try:
datas = copy_metadata('google-cloud-core')
except:
datas = copy_metadata('google-cloud-speech')
My personal solution:
Change all calls to get_distribution with it returned values (0.21.0 in my case)
Remove from pkg_resources import get_distribution from import
for all files in the package.
I had the exact same issue. I solved it by doing this:
Goto the Pyinstaller hooks folder (~\Lib\site-packages\PyInstaller\hooks)
Find the file hook-google.cloud.py, open it, and add the following code to it
datas += copy_metadata('google-cloud-translate')
datas += copy_metadata('google-api-core')
The issue seems to be that get_distribution is not working with the default google.cloud.translate hook, so I just added this to a hook that was working.
Hope this helps someone.
I was using PyCharm venv for my project, and the only solution that worked for me is changing the project over to a system interpreter (and install the required packages to that).
Today, when I tried to build an EXE out of my Python script, I got the same error:
pkg_resources.DistributionNotFound: The 'google-cloud-core' distribution was not found and is required by the application
I thought the reason was one of the listed ones in this thread, since I was sure I had all dependencies installed with pipenv because my code compiled and I could debug and run the code without issues. Note I used pipenv shell in an empty folder and created my app in it, installing all necessary libraries using pipenv install ..., and one of the libraries was google-cloud-dialogflow (the app is a chatbot manager).
The solution was simply to run pipenv install google-cloud-core.
Now, pyinstaller chatbot_manager.py --onefile --windowed created the c:\Users\...\dist\chatbot_manager.exe file without issues.
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.
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')
When I try to run any of my app engine projects by python GoogleAppEngineLauncher
I got the error log as follows:
Does anyone have any ideas of what's going on?
I tried remove the SDK and reinstall it. Nothing happens. Still got the same error.
Everything is working fine and I don't think I made any changes before this happens.
The only thing that I can think of is that I install bigquery command line tool before this happens. But I don't think this should be the reason of this.
bad runtime process port ['']
Traceback (most recent call last):
File
"/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/_python_runtime.py",
line 197, in
_run_file(file, globals()) File "/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/_python_runtime.py",
line 193, in _run_file
execfile(script_path, globals_) File "/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/runtime.py",
line 175, in
main() File "/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/runtime.py",
line 153, in main
sandbox.enable_sandbox(config) File "/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py",
line 159, in enable_sandbox
import('%s.threading' % dist27.name) File "/Users/txzhang/Documents/App/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py",
line 903, in load_module
raise ImportError('No module named %s' % fullname) ImportError: No module named google.appengine.dist27.threading
The most probable reason is having another python package that is coming from google. Run
python in verbose mode with python -vvvvv and try following command import google.
If the above import is successful make sure its coming from /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google which is the path of python google appengine libraries on my system (OSX, fresh install of google appengine sdk).
If that is not the case (but the import is successful) then its most likely other existing google libraries which are creating issue and the path of the same would be visible on python prompt after the successful import google command like following:
>>> import google
import google # loaded from Zip
/Library/Python/2.7/site-packages/protobuf-2.4.1-py2.7.egg/google/init.pyc
In this case my google protobuf package was the culprit.
Solution:
Use virtualenv: If you haven't used python virtualenv before, may be this is the best time to use it. (a) Make sure that your PYTHONPATH variable is not set to include anything that has a google package! Unset it running unset PYTHONPATH (in your bash prompt) unless you are very sure what you have there. (b) Create a new python virtualenv, activate it and try to run google appengine commands in it:
virtualenv --no-site-packages /tmp/googleapps
source /tmp/googleapps/bin/activate
dev_appserver.py path_to_google_app
Remove conflicting packages from path: Move conflicting packages a new virtualenv. This is likely to break other stuff so not recommended.
I had hit the same issue today. This being the top result on google but lacked any answers, I add this after fixing same issue on my system. There may be other possible reasons which thankfully I haven't come across.
Good luck!
A recent upgrade of the development SDK started causing this problem for me. After much turmoil, I found that the problem was that the SDK was in a sub-directory of my project code. When I ran the SDK from a different (parent) directory the error went away.