Function call and file path confusion - python

I have two scripts, main and statistics. In one script, main, several functions from the other script, statistics, are called. The code is as follows.
if initial_action == "get_portfolio_statistics":
statistics.get_portfolio_status()
statistics.get_current_securities()
statistics.get_open_trades()
Of course, the functions match as far as calling names go, and my IDE (PyCharm) is not giving any warnings. Tests have, refactor after refactor, turned up the fundamentally same error report:
Traceback (most recent call last):
File "G:/stuff/dev/!projects+repositories/RYZ/main.py", line 2, in <module>
import statistics
File "G:\stuff\dev\!projects+repositories\RYZ\statistics.py", line 1, in
<module>
import main
File "G:\stuff\dev\!projects+repositories\RYZ\main.py", line 12, in <module>
statistics.get_portfolio_status()
AttributeError: module 'statistics' has no attribute 'get_portfolio_status'
There are, however, a few intriguing recurrences that have popped up. First of all, in some cases, for no identifiable reason, the tests check out when the function names are changed, however, the results are not consistent with further tests. Second of all, the use of back/forward slashes in the file paths are not consistent, with a G:/ with the first call, and then G:\ for the two latest calls, though the first and latest calls are referring to the same file.
My question is whether this error is a matter of function naming, a matter of file path inconsistencies, a matter of cross-imports, or due to the fact that the functions being called are not housed in a class.
Project Structure:
ryz server backend (RYZ directory)
- main.py
- statistics.py
Import Structure:
# in main.py
import statistics
# in statistics.py
import main
statistics.py Structure:
<imports...>
def get_portfolio_status():
<code>
def get_current_securities():
<code>
def get_open_trades():
<code>

I'll bet it's because of the cross-import. Try to move anything that statistics.py require to that file and then import into and call from main.py.
Your function naming is normal.
I wouldn't worry about the mix of slashes. However, if you're constructing a path yourself, use the os module:
import os
path = os.path.join(os.getcwd(), 'dir', 'filename')
This will ensure that you get a path suited to your platform.

Related

Import class from folder python

Please don't immediately mark this as a duplicate question, because I have looked through loads of past questions with the same idea, but I don't understand any of the answers - they are bitty at best and involve making a file called init.py in the folder I think? I'm not sure.
So my specific problem is this: I am trying to write a program for blackjack. I have already created a class to create decks of cards in a separate file. I have everything in the same folder, called "classes". In this folder, there is:
- A file called __init.py (empty cause I don't know what it is supposed to do)
- A file called playingcardsclasses.py containing classes Card and Deck
- A file called blackjack.py which is where I am writing my main program.
I am using Pycharm, and it throws out a Traceback immediately, but still runs the code like I want - so I don't know why it is breaking.
The first line of my code is:
from playingcardsclasses.py import Deck
The traceback says:
Traceback (most recent call last):
File "(directory)/classes/blackjack.py", line 1, in
from playingcardsclasses.py import Deck
ModuleNotFoundError: No module named 'playingcardsclasses.py'; 'playingcardsclasses' is not a package
And then proceeds to run through the rest of my code happily, before finishing with a
Process finished with exit code 1
message - i.e. there is a problem here, hence the traceback.
Firstly, the syntax of your __init__.py file in your classes directory is incorrect. It should be __init__.py, not __init.py.
The reason for an __init__.py file in a directory is to tell python that this directory should be treated as a package. I.e., other .py files can be imported from this directory. (see here)
In your case, as your playingcardsclasses.py file contains the two classes, Card and Deck, you can import them from another file. Let's say you're in another file in the same directory called my_file.py. If you wanted to import the Deck class from playingcardsclasses.py, you would use:
from .playingcardsclasses import Deck
Note here that you should never use a trailing .py when importing, which is a mistake you made in your code block.
Edit: Also note the leading . in the import. This just specifies that the file playingcardsclasses.py is located in the same directory.

Does a package 'see' itself in __init__.py?

I have a flask app with the root folder called project_folder.
A code snippet from the __init__.py file of this project_folder package:
#jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
jti = decrypted_token['jti']
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
from project_folder.Controller.root import root
from project_folder.Controller import auth_controller
from project_folder.Controller import item_controller
Now the interesting thing is, that the project_folder package naturally has other smaller packages itself, which I'm importing to use them (for REST resources in this example). These are the last 3 lines, nothing throws an error so far.
But, if you take a look at the annotated function (in this example it always runs before some kind of JWT Token is being used), I am returning some inner package's function. Now when the logic truly runs this part the code breaks:
PROJECT_ROUTE\project_folder\__init__.py", line 38, in check_if_token_in_blacklist
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
NameError: name 'project_folder' is not defined
After thinking about it, it seems understandable. Importing from project_folder does import from the __init__.py file of the package, which is the actual file the interpreter currently is. So removing the package name prefix form the
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
to
return Model.RevokedTokenModel.is_jti_blacklisted(jti)
does not throw an error anymore.
The question is: Why is it only a problem inside the callback function and not with the last 3 imports?
This has to do with circular imports in python. Circular import is a form of circular dependency, created at the module import level.
How it works:
When you launch your application, python keeps a register (a kind of table) in which it records all the imported modules. When you call somewhere in your code a module, python will see in its registry if it has already been registered and loads it from there. You can access this registry via sys.module, which is actually a dictionary containing all the modules that have been imported since Python was started.
Example of use:
>>> import sys
>>> print('\n'.join(sys.modules.keys()))
So, since Python is an interpreted language, reading and execution of code is done line by line from top to bottom.
In your code, you put your imports at the bottom of your __init__.py file.
While browsing it, when python arrives at the line return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti), it will look if the module exists in its register. Which is clearly not yet the case. That's why he raises an NameError: name 'project_folder' is not defined exception.

Custom Python module structure

I am currently doing a personal coding project and I am trying to build a module, but I don't know why my structure doesn't work the way it's supposed to:
\mainModule
__init__.py
main.py
\subModule_1
__init__.py
someCode_1.py
someCode_2.py
\subModule_2
__init__.py
otherCode.py
I want to be able to run the following code from main.py:
>>> from subModule_1 import someCode_1
>>> someCode_1.function()
"Hey, this works!"
>>> var = someCode_2.someClass("blahblahblah")
>>> var.classMethod1()
>>> "blah blah blah"
>>> from subModule2 import otherCode
>>> otherCode("runCode","#ff281ba0")
However, when I try to import someCode_1, for example, it returns an AttributeError, and I'm not really sure why. Is it to do with the __init__.py file?
REVISIONS
Minimal, Complete and verifiable (I hope...)
\mainDir
__init__.py # blank file
main.py
\subDir
__init__.py # blank file
codeFile.py
Using this...
#main.py file
import subDir
subDir.codeFile.function()
And this...
#codeFile.py file
def function():
return "something"
...it returns the same problem mentioned above**.
** The exact error is:
Traceback (most recent call last):
File "C:\...\mainDir\main.py", line 2, in <module>
subDir.codeFile.function()
AttributeError: module 'subDir' has no attribute 'codeFile'
Credits to #jonrsharpe: Thanks for showing me how to use Stack Overflow correctly.
You have two options to make this work.
Either this:
from subdir import codeFile
codeFile.function()
Or:
import subdir.codeFile
subdir.codeFile.function()
When you import subDir, it does three things:
executes the code in mainDir/subDir/__init__.py (i.e. in this case does nothing, because this file is empty)
imports the resulting module under the name subDir locally, which will in turn make it an attribute of the mainDir module;
registers the new import globally in the sys.modules dictionary (because the import is being performed from a parent module mainDir, the name is completed to 'mainDir.subDir' for the purposes of this registration);
What it does not do, because it hasn't been told to, is import subDir.codeFile. Therefore, the code in codeFile.py has not been run and the name codeFile has not yet been imported into the namespace of mainDir.subDir. Hence the AttributeError when trying to access it. If you were to add the following line to mainDir/subDir/__init__.py then it would work:
import codeFile
Specifically, this will:
run the code in codeFile.py
add the resulting module as an attribute of the mainDir.subDir module
store a reference to it as yet another entry in sys.modules, this time under the name mainDir.subDir.codeFile.
You could also achieve the same effect from higher up the module hierarchy, by saying import subDir, subDir.codeFile instead of just import subDir in your mainDir.main source file.
NB: When you test this from the command line or IDE, make sure that your current working directory (queried by os.getcwd(), changed using os.chdir(wherever) ) is neither mainDir nor subDir. Work from somewhere else—e.g. the parent directory of mainDir. Working from inside a module will lead to unexpected results.

Python 3 modules and package relative import doesn't work?

I have some difficulties constructing my project structure.
This is my project directory structure :
MusicDownloader/
__init__.py
main.py
util.py
chart/
__init__.py
chart_crawler.py
test/
__init__.py
test_chart_crawler.py
These are codes :
1.main.py
from chart.chart_crawler import MelonChartCrawler
crawler = MelonChartCrawler()
2.test_chart_crawler.py
from ..chart.chart_crawler import MelonChartCrawler
def test_melon_chart_crawler():
crawler = MelonChartCrawler()
3.chart_crawler.py
import sys
sys.path.append("/Users/Chois/Desktop/Programming/Project/WebScrape/MusicDownloader")
from .. import util
class MelonChartCrawler:
def __init__(self):
pass
4.util.py
def hi():
print("hi")
In MusicDownloader, when I execute main.py by python main.py, it shows errors:
File "main.py", line 1, in <module>
from chart.chart_crawler import MelonChartCrawler
File "/Users/Chois/Desktop/Programming/Project/WebScrape/MusicDownloader/chart/chart_crawler.py", line 4, in <module>
from .. import util
ValueError: attempted relative import beyond top-level package
But when I execute my test code in test directory by py.test test_chart_crawler.py, it works
When I first faced with absolute, relative imports, it seems like very easy and intuitive. But it drives me crazy now. Need your helps. Thanks
The first problem is MusicDownloader not being a package. Add __init__.py to MusicDownloader along with main.py and your relative import ..chart should work. Relative imports work only inside packages, so you can't .. to non-package folder.
Editing my post to provide you with more accurate answer to your answer edit.
It's all about the __name__. Relative imports use __name__ of the module they are used in and the from .(.) part to form a full package/module name to import. Explaining in simple terms importer's __name__ is concatenated with from part, with dots showing how many components of name to ignore/remove, i.e.:
__name__='packageA.packageB.moduleA' of the file containing line: from .moduleB import something, leads to combined value for import packageA.packageB.moduleB, so roughly from packageA.packageB.moduleB import something(but not absolute import as it would be if typed like that directly).
__name__='packageA.packageB.moduleA' of the file containing line: from ..moduleC import something, leads to combined value for import packageA.moduleC, so roughly from packageA.moduleC import something(but not absolute import as it would be if typed like that directly).
Here if it's a moduleB(C) or a packageB(C) doesn't really matter. What's important is that we still have that packageA part which works as an 'anchor' for relative import in both cases. If there will be no packageA part, relative import won't be resolved, and we'll get an error like "Attempted relative import beyond toplevel package".
One more note here, when a module is run it gets a special __name__ value of __main__, which obviously prevents it from solving any relative imports.
Now regarding your case try adding print(__name__) as the very first line to every file and run your files in different scenarios and see how the output changes.
Namely if you run your main.py directly, you'll get:
__main__
chart.chart_crawler
Traceback (most recent call last):
File "D:\MusicDownloader\main.py", line 2, in <module>
from chart.chart_crawler import MelonChartCrawler
File "D:\MusicDownloader\chart\chart_crawler.py", line 2, in <module>
from .. import util
ValueError: Attempted relative import beyond toplevel package
What happened here is... main.py has no idea about MusicDownloader being a package (even after previous edit with adding __init__.py). In your chart_crawler.py: __name__='chart.chart_crawler' and when running relative import with from .. the combined value for package will need to remove two parts (one for every dot) as explained above, so the result will become '' as there're just two parts and no enclosing package. This leads to exception.
When you import a module the code inside it is run, so it's almost the same as executing it, but without the __name__ becoming __main__ and the enclosing package, if there's any, being 'noticed'.
So, the solution is to import main.py as part of the MusicDownloader package. To accomplish the described above, create a module, say named launcher.py on the same level of hierarchy as MusicDownloader folder (near it, not inside it near main.py) with the following code:
print(__name__)
from MusicDownloader import main
Now run launcher.py and see the changes. The output:
__main__
MusicDownloader.main
MusicDownloader.chart.chart_crawler
MusicDownloader.util
Here __main__ is the __name__ inside launcher.py. Inside chart_crawler.py: __name__='MusicDownloader.chart.chart_crawler' and when running relative import with from .. the combined value for package will need to remove two parts (one for every dot) as explained above, so the result will become 'MusicDownloader' with import becoming from MusicDownloader import util. And as we see on the next line when util.py is imported successfully it prints its __name__='MusicDownloader.util'.
So that's pretty much it - "it's all about that __name__".
P.S. One thing not mentioned is why the part with test package worked. It wasn't launched in common way, you used some additional module/program to lauch it and it probably imported it in some way, so it worked. To understand this it's best to see how that program works.
There's a note in official docs:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.

Import with files names conflict

I'm trying to develop a simulation class that replaces serial at specific apps(Win7, python 2.7).
I have a SerialHandle class that works in number of apps, It's job is add logic to the well known serial methods, the idea was to replace python serial.py with a dummy file with the same name so we won't have to change and imports at SerialHandle.
Now i have this file with Serial class just like the origin and it works fine:
serial.py
...Serial()
Since i want to really simulate the methods i need the SerialException from serialutil
so inside my serial.py i'm trying to import it using:
from serial import SerialException
But as expected i'll get this raise since from serial goes to the local file at first:
Traceback (most recent call last):
File "C:/CROW/ATE/DUTDrivers/DD_SimulatorExample/DD_SimulatorExample.py", line 18, in <module>
from Utilities.Serial.SerialHandle.trunk.SerialHandle import SerialHandle
File "C:\CROW\ATE\Utilities\Serial\SerialHandle\trunk\__init__.py", line 4, in <module>
from Utilities.Simulator import serial
File "C:\CROW\ATE\Utilities\Simulator\serial.py", line 11, in <module>
from serial import SerialException
ImportError: cannot import name SerialException
I understand the problem is the file name since at any other file it will work...
I've tried sys.append(site-packages....serial.py) no luck.
Questions:
Any way to tell the interpreter to ignore the local file at a specific from..import?
Is there any other way to import from an absolute path?
Notes:
the file naming as serial.py is not a decision it's a definition so changing the name is not relevant...
Overloading python serial is not an option also...
You must be using python 2.x, since absolute imports are the default in python 3.x. You can use absolute imports in your serial.py file by adding this at the top of the file:
from __future__ import absolute_import
Note that you will need to convert any implicit relative imports from your serial.py file into explicit relative imports. So if you were importing some_func from other_file.py, which is in the same directory, you would need to change that to:
from .other_file import some_func
Note that the "." indicates a relative import from the same package as the current file. See here for additional detail.

Categories