I found that when I run my scripts from environment - everything is OK.
But, when I try to run them from bash - I receive different errors with import modules. (ModuleNotFoundError, ImportError)
I didn't set the environment at all, so, please, tell me, what I should configure to succeed with it.
I use python 3.7
Received the next error:
Traceback (most recent call last):
File "main.py", line 1, in <module> from package_3.file3 import *
ModuleNotFoundError: No module named 'package_3'
The structure is:
package_1
file1.py
package_2
file2.py
package_3
file3.py
package_4
file4.py
What happens is, probably, that your working environment is python3, and when you run things in the shell, it's using python2. To solve it, run your script with python3 in the shell, as in:
python3 my_script.py
Related
I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.
Why I can import the package in the parent directory in PyCharm, however, it reports an error in the terminal?
I will use the following toy code to show my problem. For example, the project structure is like the following:
In hello.py is:
def func():
print("hello")
In test.py is
import mypackage.hello
mypackage.hello.func()
Now if I run test.py in PyCharm, it runs perfectly and prints hello. However, if I use the terminal and cd to test directory, and run command python test.py, it reports the following error:
Traceback (most recent call last):
File "test/test.py", line 1, in <module>
import mypackage.hello
ModuleNotFoundError: No module named 'mypackage'
PS: I use the same environment in both PyCharm and terminal.
Question:
Why does it show different results in PyCharm IDE and terminal?
In general, what's the correct style to import a package in the parent directory such that I can run both in the terminal and IDE?
I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")
I am getting below ImportError while running a python file.
ImportError: No module named osgeo.gdal_array
!/usr/bin/env python
from osgeo.gdal_array import BandReadAsArray
However, if i try to import same from command line, it runs fine.
$ which python
/home/hduser/anaconda2/bin/python
$ python
>>> from osgeo.gdal_array import BandReadAsArray
>>>
Also, please see the below where i am getting the same ImportError.
$ /usr/local/bin/python2.7
>>> from osgeo.gdal_array import BandReadAsArray
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named osgeo.gdal_array
I figured out that there is something going on between different versions of python. But, i do not want to change the original source code.
How do i make my program run without changing anything within the code of calling python installed in anaconda explicitly?
I'm trying to run some tests in PyCharm using Behave and it keeps telling me I have this issue:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/behave", line 7, in <module>
from behave.__main__ import main
ImportError: No module named behave.__main__
I had a colleague clone the project and they can run it fine on their machine, any insights?
Python 2.7.10
$ pip freeze | grep ehave
behave==1.2.5
The issue is that in Project Interpreter, I was using a different version of Python on my machine that did not have behave installed/included.
From Pycharm settings -> Project -> Python interpreter , search for behave and include it into your ptoject.