I am trying to understand PYTHONPATH related to my project.
My project is in the dir $HOME/Programs/medusa-2.0, and my source files are in $HOME/Programs/medusa-2.0/medusa.
I have set my PYTHONPATH in the .bashrc like this:
export MEDUSA_HOME=$HOME/Programs/medusa-2.0
export PYTHONPATH=${MEDUSA_HOME}/medusa:${PYTHONPATH}
When I try to import a class, from system import error_handler, hello, I get errors saying that it cannot find the function execute_command. I don' t understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in medusasettings?
ImportError Traceback (most recent call last)
<ipython-input-2-7f959e81c735> in <module>()
----> 1 from medusasystem import error_handler, hello
/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py in <module>()
9 from local import lcat
10 import psutil
---> 11 import ranking
12 import settings
13 import simplejson as json
/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py in <module>()
7 import cache
8 from decors import make_verbose
----> 9 from scheduler.predictionranking import get_prediction_metrics
10 from scheduler.randomranking import get_random_metrics
11 from settings import medusa_settings
/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py in <module>()
6
7 from celery import task
----> 8 import hdfs
9 from networkdaemon import read_network_data
10 from numpylinearregression import estimate_job_execution, calculate_linear_regression_numpy
/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py in <module>()
4 from hadoopy._hdfs import _checked_hadoop_fs_command
5 from celery import task
----> 6 from medusasystem import execute_command
7 import settings
8
ImportError: cannot import name execute_command
I have tried to launch a python file with python -v, and I've got this error:
# /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc matches /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py
import hdfs # precompiled from /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc
Traceback (most recent call last):
File "tests/testHello.py", line 3, in <module>
from medusasystem import error_handler, hello
File "/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py", line 11, in <module>
import ranking
File "/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py", line 9, in <module>
from scheduler.predictionranking import get_prediction_metrics
File "/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py", line 8, in <module>
import hdfs
File "/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py", line 6, in <module>
from medusasystem import execute_command
ImportError: cannot import name execute_command
If I launch my virtualenv for my project, shouldn't the PYTHONPATH be defined inside the virtualenv?
With the information presented, I think you are just importing it from the wrong place: "I don't understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in medusasettings?" but in the trace(s) there are from settings import medusa_settings and from medusasystem import execute_command. Verify that execute_command is in medusasystem.
virtualenv is not going to change PYTHONPATH, so it will be the same after activation (unless of course, you do something like what is in the next sentence). If your question is about setting it when using the virtualenv, see this answer: How do you set your pythonpath in an already-created virtualenv?. By doing it in .bashrc, you are defining it when you open the shell rather than attaching it to the virtualenv script(s).
The problom was with circular import issue. I replaced:
from medusasystem import execute_command
with
import medusasystem
execute_command = medusasystem.execute_command
and it worked.
Related
I am trying to import a module from an external file as follows:
>>> from sys import path
>>> path.append("/tmp/security/")
>>> from util import set_archive
When I run this as root it works fine. But when I run this as testuser it does not work.
permissions on /tmp/security/util.py is as follows:
[root#dashmpp-head-0 - Db2wh /]# ls -l /tmp/security/util.py
-rwx------ 1 db2inst1 root 88046 Dec 4 19:08 /tmp/security/util.py
when I try to import as testuser I get:
>>> from sys import path
>>> path.append("/tmp/security/")
>>> from util import set_archive
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'util'
>>>
testuser belongs to another groups called db2iadm1
Is there any way I can import the module as root so I can use set_archive function?
I'm just learning Python.
I have a file with the following content
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
If I name this file csv2.py and call:
python csv2.py
... it works. But if I name this file csv.py and run:
python csv.py
It triggers this exception:
C:\Git\algotrading [master ≡ +3 ~0 -0 !]> python csv.py
Traceback (most recent call last):
File "csv.py", line 2, in <module>
import matplotlib.pyplot as plt
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\collections.py", line 36, in <module>
import matplotlib.mlab as mlab
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\mlab.py", line 172, in <module>
import csv
File "C:\Git\algotrading\csv.py", line 2, in <module>
import matplotlib.pyplot as plt
AttributeError: module 'matplotlib' has no attribute 'pyplot'
It took me like 40 minutes to figure out the problem. I mean.. Figure out the problem had to do with the name of the file.
Why is that happening?
It looks like matplotlib.pyplot through various imports needs mlab.py which calls "import csv". This should find a file (that is not yours) called csv but since you have renamed your file to csv.py it is attempting to import that instead, overriding the required import and messing up the import for matplotlib.pyplot.
csv.py is built into python and thus is restricted.
If you run a python interpreter and try to import csv, you'll succeed without having to download anything new.
Like igoldthwaite and Daniel Dobalian said:
If you create a file named: csv.py with the content:
import csv
print(csv)
And run:
python csv.py
You will see that this file import itself:
<module 'csv' from '/home/your/folder/csv.py'>
Also, if you do the same thing with other modules, you will find the same result:
import subprocess
print(subprocess)
Result:
<module 'subprocess' from '/home/your/folder/subprocess.py'>
And finally, if you do it with a file named matplotlib.py, you will get the same result:
import matplotlib
print(matplotlib)
# <module 'matplotlib' from '/home/your/folder/matplotlib.py'>
I have this project structure:
root_package/
root_package/packA/
root_package/packA/__init__.py (empty)
root_package/packA/moduleA.py
root_package/packB/__init__.py (empty)
root_package/packB/moduleB.py
root_package/rootModule.py
In the rootModule.py I have from packA.moduleA import ModuleAClass.
At the packA.moduleA.py I have this from root_package.packB.moduleB import ModuleBItem.
When running rootModule either via PyCharm or the terminal with python ./rootModule.py I am getting this error:
Was this the right way of importing?
Traceback (most recent call last):
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "/project_dir/rootPackage/packA/moduleA.py", line 8, in <module>
from rootPackage.packB.moduleB import module_b_method
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.wavelet_compression import WaveletCompression
ImportError: cannot import name WaveletCompression
How to solve this?
Update 1
I've added a test file at the project_folder (not the root_package folder).
So the current directory structure is this:
project_folder/
project_folder/root_package/
project_folder/root_package/packA/
project_folder/root_package/packA/__init__.py (empty)
project_folder/root_package/packA/moduleA.py
project_folder/root_package/packB/__init__.py (empty)
project_folder/root_package/packB/moduleB.py
project_folder/root_package/rootModule.py
project_folder/test_rootModule.py
I haven't made the project_folder a package (no __init__.py file) since, the test_rootModule is simply a script to help me run the experiments.
So, in root_package/packA/moduleA.py, after changing the from root_package.packB.moduleB import ModuleBitem, to from packB.moduleB import ModuleBitem, as the answer suggests, it works.
But now there are two problems:
1. PyCharm doesn't agree with the change:
I cannot run my experiments from the project_folder/test_rootModule.py script.
I got this error:
Traceback (most recent call last):
File "project_folder/test_rootModule.py", line 8, in
from root_package.rootModule import rootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB
I cannot seem to get the 2nd Traceback to look like a code segment.
Update 2
What solved the problem was going to the Project: project_name > Project Structure dialog in PyCharm, selecting the root_package and then setting it as a Sources folder.
Now, I can run via the IDE both the rootModule and the test_rootModule.
Although, I cannot get to run the test_rootModule from the terminal.
The test_rootModule has these imports:
from root_package.rootModule import RootModuleClass
from root_package.packB.moduleB import module_b_item
I am at the project_folder dir, and run python ./test_rootModule.py and get this error:
Traceback (most recent call last):
File "./test_rootModule.py", line 8, in <module>
from root_package.rootModule import RootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in <module>
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB
If you are running all your code from within this path:
project_folder
Then you should ensure that all your modules that reside in root_package are referenced by that first. So for example:
from root_package.modA import foo
I'm trying to run a script in python, and I'm getting an "invalid elf header" error. I have a series of scripts that call one another, I'll go ahead and include those as well:
import sys
sys.path.append("/home/smh/Linux/Desktop/gras-03-03/python")
from python_utilities import GRASTestor
which calls GRASTestor.py
from BaseTestor import BaseTestor
import numpy as np
import hepunit as unit
from gdml_writer import gdml_writer
from GDMLGeometryBuilder import GDMLGeometryBuilder
from GRASMacroBuilder import GRASMacroBuilder,GRASRMCMacroBuilder
from Plotters import Plotter
import os
import sys
import SpenvisCSVFileHandler
which calls SpenvisCSVFileHandler.py
import string
import Spenvis.so
import os
from numpy import *
which is where we get our error, specifically with the line "import Spenvis.so"
/home/smh/Linux/Desktop/gras-03-03/python/python_utilities
Traceback (most recent call last):
File "perform_gras_rmc_tests.py", line 6, in <module>
from python_utilities import GRASTestor
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/GRASTestor.py", line 19, in <module>
import SpenvisCSVFileHandler
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/SpenvisCSVFileHandler.py", line 8, in <module>
import Spenvis.so
ImportError: /home/smh/Linux/Desktop/gras-03-03/python/python_utilities/Spenvis.so: invalid ELF header
And I'm not certain why it's not working. Any suggestions would be appreciated!
Never mind. Upon looking at the file architecture, it appears the file Spenvis.so is mac specific for some reason. Just need to get the correct file, then.
I encoutner the following import resolution error in a program of mine:
I am: homie.translators.is24.rest.translator
Traceback (most recent call last):
File "/usr/local/sbin/is24rest", line 3, in <module>
from homie.interfaces.is24.rest import __main__
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/__init__.py", line 8, in <module>
from .exporter import Exporter
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/exporter.py", line 12, in <module>
from homie.translators.is24.rest.translator import Translator
File "/usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py", line 9, in <module>
from .factories.rest.restFactory import RestFactory
ImportError: No module named factories.rest.restFactory
The content of /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py is
"""
Created on 18.06.2014
#author: Richard Neumann
"""
print('I am: ' + str(__name__))
from homie.translators.abc import Translator as T
from .factories.rest.restFactory import RestFactory
from .factories.openimmo.openimmoFactory import OpenImmoFactory
class Translator(T):
<snip>
Also, the relative module exists:
root#srv:/usr/src/is24-translator# ls /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/factories/rest/
abc.py attachments __init__.py __pycache__ realestates restFactory.py
Why does python3 try to do an absolute import here?
I do only encounter this problem under Debian 7 with python 3.2.3.
Under Arch w/ python 3.4.1 it works just fine.
Am I missing something?
There was the __init__.py missing in .factories, which caused this error.
Took me a while to realize that.